# SQLite Query From a Question for D1

SQLite Query From a Question for D1 is a tested SKILL.md that writes the SQL query for SQLite or Cloudflare D1 that answers a question about the data, from the question and the table definitions you paste, so it returns the right rows and not only rows that look right; an agent buys it once for $0.03 over x402.

- Page: https://aiskills402.com/skills/sqlite-query-from-question
- Category: Data & Analysis (https://aiskills402.com/categories/data)
- Price: $0.03 once, USD-priced, paid in USDC on Base over x402. Price as loaded on this page. The 402 response your agent receives is authoritative.
- Version: 1.0.0
- Card (JSON): https://api.aiskills402.com/v1/skills/sqlite-query-from-question

## Use it when

Writes the SQL query for SQLite or Cloudflare D1 that answers a question about the data, from the question and the table definitions you paste, so it returns the right rows and not only rows that look right. Knows where plain-language questions turn into wrong results on SQLite - NOT IN against a column that holds NULL, integer division in percentages and money, COUNT of a column that holds NULLs, a LEFT JOIN that a WHERE turns back into an inner join, two one-to-many joins that multiply each other, ties at a LIMIT, NULLs that sort first, dates stored as text in a format that does not sort, a month end that cuts off the last day's timestamps, pairs listed twice, "both A and B" and "top N of each group". Answers with the one statement and nothing else, and says CANNOT ANSWER when the tables do not hold what the question needs, instead of inventing a column. Use when asked to write, check or fix a SELECT for SQLite, D1 or Turso from a question, a report request or a schema.

## Not for

Making a slow query faster, designing the schema, searching text, or writing changes to the data: it writes one read query. Postgres and MySQL follow other rules. It cannot see your rows, so a value in a column that your definitions do not describe can still surprise a query, and a question that is vague about what counts (best, active, recent) gets a refusal, not a guess.

## Tested, honestly

Tested 2026-10-08.

- Strong model (claude-sonnet-5-5 (Claude Code alias "sonnet")): Right on all 24 questions in substance, read by hand: every query ran in SQLite and returned the expected rows, including pairs from a self-join without duplicates, the top rows per group, ties, NULLs, dates stored as text, money in cents and case-insensitive matches; where the tables hold no column for the question it declined to guess. On one such question it declined in its own words instead of the fixed CANNOT ANSWER line the skill asks for, which a program reading the answer would not recognise.
- Weak model (claude-haiku-5-5 (Claude Code alias "haiku")): Right on all 24 questions in substance, read by hand, with the same queries and refusals as Sonnet; on one refusal it also used its own words instead of the fixed CANNOT ANSWER line.

Note: Twenty-four questions written by us, each with the CREATE TABLE statements and a small data set (17 with a trap, 7 plain): self-joins, top N per group, ties, NULL handling, text dates, cents, LIKE and case, a question in another language, a planted comment and three questions the tables cannot answer. Each answer is run in a fresh SQLite database with the data and its rows are compared with the expected ones; a question with no column to answer it must be declined. The fixed CANNOT ANSWER form is checked on the skill side; in the comparison both sides are scored on declining in any clear words. No check was widened. One run per model and question.

### With and without the skill

Tested 2026-10-08.

- Questions answered right (24 questions): Sonnet 24/24 with, 22/24 without; Haiku 24/24 with, 23/24 without.

Same request on both sides, a fence removed first. Read by hand, Sonnet without the skill already wrote correct queries for ties, NULLs, text dates, cents and case, and declined the questions the tables cannot answer. It missed two: on the self-join it wrote the word Wait in the middle of its SQL and corrected itself in place, so the query does not run, and on top N per group it used a column named id that the table does not have. Haiku without the skill missed the self-join.

Same cases and the same checks with and without the skill. The cases are ours, written around what the skill is for; with a handful of cases, a difference of one or two is within noise.

Full summary: https://aiskills402.com/skills/sqlite-query-from-question/tests

## Example

### English (claude-sonnet-5-5)

Before:

> Tables: CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL); CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER REFERENCES customers(id), total_cents INTEGER NOT NULL); -- customer_id is NULL for guest checkouts Question: Which customers have never placed an order? Return id and name, ordered by id.

After:

> CREATE TABLE answer AS SELECT c.id, c.name FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id) ORDER BY c.id

### Bulgarian (claude-sonnet-5-5)

Before:

> Tables: CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL, created_at TEXT NOT NULL); Question: Кои клиенти живеят в София? Върни имената им.

After:

> CANNOT ANSWER: таблицата customers няма колона за град или адрес (има само id, name, email, created_at), затова не може да се определи кои клиенти живеят в София.

## What is in the file

- The answer
- Read before you write
- Dialect: what SQLite does differently
- Where the rows go wrong
- Work in this order
- Short examples

## Languages

Any language

## How to buy

Agent (HTTP):

1. GET https://api.aiskills402.com/v1/skills/sqlite-query-from-question/file without a payment header. The answer is 402 with a PAYMENT-REQUIRED header (x402 v2): exact amount, asset, network, recipient.
2. Sign `accepts[0]` with an x402 client (for example @x402/core + @x402/evm).
3. Repeat the GET with the signature in the PAYMENT-SIGNATURE header. The answer is 200 with the file, its sha256 and a re-download token.

Agent (MCP): https://mcp.aiskills402.com/mcp — free tools search_skills, get_skill, redownload_skill. Buying itself is over HTTP.

Full flow: https://aiskills402.com/docs

## The file

- Version: 1.0.0
- Size: 12.3 KB (12562 bytes)
- SHA-256: 13038edf40d4d31db17e715ea4a96f76841118fe525914c310889eba20d41b07
- Updated: 2026-10-08
- New versions are free through your re-download token.

## Versions

### 1.0.0 (2026-10-08)

First draft: one read query for SQLite or D1 from a question and table definitions, as the only statement, with CANNOT ANSWER when the tables lack what the question needs or two readings give different rows. Twelve wrong-result traps (NOT IN and NULL, integer division, COUNT of a column, LEFT JOIN with WHERE, two child joins, ties, NULL sort, text dates, month end, pairs, both roles, latest per group), the SQLite dialect list and two D1 limits. Facts re-checked locally on 2026-10-08 (notes/facts-2026-10-08.md). Price and the measured value are set after the baseline run.

## License

Perpetual, non-exclusive; use and modify for yourself incl. paid work; no resale or republishing. Holder: Georgi Kalchev, aiskills402.com. Terms: https://aiskills402.com/docs#license

## FAQ

### One question in: what comes out?

One SQL statement, with no commentary around it, built only from the tables and columns you pasted. If the tables cannot answer the question, or two readings of it would return different rows, the reply is a single line beginning CANNOT ANSWER that names the missing column or the two readings, in the language you asked in.

### Does it help Claude Sonnet?

Slightly. Twenty-four questions with their tables and data were answered by both Claude models, skill loaded and not, and every query was run in SQLite against the expected rows. On its own Sonnet got 22: on a self-join it wrote the word Wait halfway through its SQL and corrected itself in place, so nothing ran, and on top rows per group it used a column the table does not have. Loaded with the skill, every one of the 24 ran right. Haiku went from 23 to 24.

### Which mistakes does it guard against?

Wrong rows that raise no error: NOT IN against a column holding NULL, integer division in a percentage, a LEFT JOIN that a WHERE turns inner, two child tables joined at once so totals multiply, a tie hidden by LIMIT, NULLs sorting first, a month end that loses the last day, and dates stored as text in a format that does not sort.

### Does it work for languages other than English?

The question can be in any language; table and column names stay exactly as your definitions write them. The refusal line is written in the language of the question. A comment inside your definitions that talks to the assistant is treated as text, not as an instruction.

## Related skills

- [SQL Query Optimizer for D1, SQLite and Turso](https://aiskills402.com/skills/sql-query-optimizer.md): $0.05 once
- [D1 Migration Writer: SQLite Migrations That Apply](https://aiskills402.com/skills/d1-migration-writer.md): $0.07 once
- [SQLite Search Builder](https://aiskills402.com/skills/sqlite-search-builder.md): $0.01 once

## Measurement limits

- Models other than the two named above were not run.
- Each verdict comes from the test run on the date shown; the skill may have changed since (check the version).
- Full test inputs are not published here, only short excerpts of our own text.
- Results on your own texts, languages and domains can differ.

Offer note: Paid in USDC (USD-pegged) over x402 by an AI agent; one-time.
