Get SQL From a Question and Check It in SQLite
Run AI-written SQL for four order questions in SQLite and compare with hand counts. The no-coupon count was one short in 6 of 6 runs; a column note fixed it.
SQL from an AI can run without an error and still return the wrong number. Count each answer by hand first, load the same table into SQLite, and compare. The mismatch shows which question went wrong.
[1] Build the table. It holds 6 made-up customers and 10 orders. The data deliberately mixes a month boundary (Oct 31 at night, Nov 1 at midnight), refunded and cancelled orders, an order with no customer, a customer with no city, and coupon cells that are NULL in most rows and an empty string ('') in one.
| Order | Customer | Ordered at | Amount | Status | Coupon |
|---|---|---|---|---|---|
| 1 | Ava | 2026-10-02 10:15:00 | 12000 | paid | NULL |
| 2 | Ava | 2026-10-31 23:30:00 | 30000 | paid | WELCOME |
| 3 | Ben | 2026-10-15 09:00:00 | 8000 | paid | '' |
| 4 | Ben | 2026-11-01 00:00:00 | 15000 | paid | NULL |
| 5 | Cara | 2026-10-20 14:00:00 | 20000 | refunded | NULL |
| 6 | Cara | 2026-10-21 16:40:00 | 25000 | paid | WELCOME |
| 7 | Dan | 2026-10-09 11:11:00 | 9000 | paid | NULL |
| 8 | Eli | 2026-09-30 23:59:59 | 7000 | paid | NULL |
| 9 | (none) | 2026-10-12 13:00:00 | 5000 | paid | NULL |
| 10 | Ava | 2026-10-31 08:00:00 | 4000 | cancelled | NULL |
In the customers table, Ava, Ben, Cara and Fay live in Seattle, Austin, Seattle and Seattle. Dan has a NULL city and Eli lives in Austin. Fay has no orders. The table is in run/data.en.sql; create it with sqlite3 :memory: < run/data.en.sql.
[2] Count the answers by hand before you ask for any SQL.
| Question | Hand count |
|---|---|
| Q1. Paid total in October, per customer | Ava 42000 (12000 + 30000), Ben 8000, Cara 25000, Dan 9000 |
| Q2. Orders placed without a coupon | 8 (7 NULL + 1 empty string) |
| Q3. Customers with no orders | Fay |
| Q4. Average paid amount per city | Seattle 22333.33, Austin 10000, no city 9000 |
For Q1, orders 5 (refunded), 10 (cancelled), 8 (September) and 4 (Nov 1, midnight) must drop out, and order 2 (Oct 31, night) must stay in.
[3] Write the prompt. It gives the table definitions and column notes, not the rows, which is how you would brief a model on a real database. The column notes are there because the Claude prompting guide recommends giving context the model would otherwise have to guess.
Write one SELECT statement per question, to run on SQLite 3. [Tables]customers(id INTEGER PRIMARY KEY, name TEXT NOT NULL, city TEXT)orders(id INTEGER PRIMARY KEY, customer_id INTEGER, ordered_at TEXT NOT NULL, amount INTEGER NOT NULL, status TEXT NOT NULL, coupon TEXT) [Columns]- orders.customer_id refers to customers.id.- orders.ordered_at is text in 'YYYY-MM-DD HH:MM:SS' format.- orders.amount is an integer in dollars.- orders.status is one of 'paid', 'refunded', 'cancelled'.- orders.coupon is the coupon code. [Questions]Q1. For orders paid (status paid) in October 2026, give the total amount per customer name. Count only orders that have a customer record.Q2. How many orders were placed without a coupon? Ignore status.Q3. List the names of customers who have not placed a single order.Q4. Give the average amount of paid orders per city. Count only orders that have a customer record. Customers with no city entered must still appear as one row. [Output format]Write exactly one sql code block per question, and make its first line a comment such as -- Q1. No explanations.[4] Run the prompt 3 times. This is what the model wrote for Q1 and Q2 (run 1; all 3 runs had the same structure, with a different column alias for Q2 in runs 2 and 3).
-- Q1SELECT c.name, SUM(o.amount) AS total_amountFROM orders oJOIN customers c ON c.id = o.customer_idWHERE o.status = 'paid' AND o.ordered_at >= '2026-10-01 00:00:00' AND o.ordered_at < '2026-11-01 00:00:00'GROUP BY c.id, c.name;-- Q2SELECT COUNT(*) AS order_countFROM ordersWHERE coupon IS NULL;The date range is half-open, not BETWEEN ... AND '2026-10-31'. In all 3 runs Q3 used NOT EXISTS and Q4 used JOIN then GROUP BY c.city.
[5] Run the SQL in SQLite and compare with the hand counts. The script extracts the -- Q1 to -- Q4 blocks, runs them on an in-memory database built from the table above, and compares the rows with the hand-counted values. It sorts the rows before comparing, so row order is not checked. That is fine here because none of the four questions asks for an order, but a question such as "highest sales first" needs an unsorted comparison.
$ python3 run/check_sql.py run/sql.en.1.out.txt --lang enQ1 match [('Ava', 42000), ('Ben', 8000), ('Cara', 25000), ('Dan', 9000)]Q2 MISMATCH query result: [(7,)] hand count: [(8,)]Q3 match [('Fay',)]Q4 match [('Austin', 10000.0), ('Seattle', 22333.33), (None, 9000.0)]All 3 runs gave the same result. The month boundary (Q1), customers without orders (Q3) and the group with no city (Q4) were right every time, and only Q2 returned 7. A Korean-language version of the same table and prompt did the same in 3 more runs: 6 of 6 runs missed Q2.
[6] Find where it missed. All runs used only WHERE coupon IS NULL, but order 3 has an empty string in the coupon cell, not NULL. The SQL syntax and the reading of the question were fine. The prompt never mentioned empty strings, so the model had no way to know. Cells like this appear when a form saves a blank field as ''.
Change one line in the column notes and leave the questions as they are.
- orders.coupon is the coupon code. Orders without a coupon have NULL or an empty string ('').Across 3 more runs, Q2 became WHERE coupon IS NULL OR coupon = '' and passed.
$ python3 run/check_sql.py run/sql2.en.1.out.txt --lang enQ1 match [('Ava', 42000), ('Ben', 8000), ('Cara', 25000), ('Dan', 9000)]Q2 match [(8,)]Q3 match [('Fay',)]Q4 match [('Austin', 10000.0), ('Seattle', 22333.33), (None, 9000.0)]With the 3 Korean runs, all 6 runs matched on all four questions.
[7] Check that the script catches wrong SQL. The statements below are not model output; they were written to be wrong: BETWEEN dropping the Oct 31 night order, IS NULL only, NOT IN over a subquery that contains NULL, and city IS NOT NULL removing the no-city group. All four were rejected, and a correct query with < '2026-11-01' was accepted.
$ python3 run/check_sql.py --test-flaws --lang en[rejected] Q1 BETWEEN '2026-10-01' AND '2026-10-31' -> [('Ava', 12000), ('Ben', 8000), ('Cara', 25000), ('Dan', 9000)][rejected] Q2 coupon IS NULL only -> [(7,)][rejected] Q3 NOT IN (customer_id) -> [][rejected] Q4 WHERE city IS NOT NULL -> [('Austin', 10000.0), ('Seattle', 22333.33)][accepted] Q1 valid (date < Nov 1) -> [('Ava', 42000), ('Ben', 8000), ('Cara', 25000), ('Dan', 9000)]NOT IN returns nothing in Q3 because the subquery contains a NULL (order 9 has no customer). The [SQLite IN operator notes](https://www.sqlite.org/lang_expr.html#in_op) cover how NULL changes the result.