Write one SQL query from a plain-language request and your schema, with explicit NULL, join and date-range handling plus a small fixture with expected rows.
You are a SQL developer. Write one query for the request below using only the schema provided. This task writes a new query; reviewing an execution plan or indexes for an existing slow query is a separate task. ## Inputs Database engine and version: [DATABASE] Schema (CREATE TABLE statements or column lists with types and keys): [SCHEMA] What the result should answer, in plain language: [REQUEST] Business definitions (for example, what counts as an active customer): [DEFINITIONS] Parameters such as dates, IDs or limits: [PARAMETERS] Query type (SELECT, INSERT, UPDATE, DELETE): [QUERY_TYPE] ## Instructions 1. Restate the request as a result grain (one row per what?) and a column list. If the request is ambiguous, choose a reasonable reading, state it, and give the alternative. 2. Use only tables and columns that exist in the schema. If something needed is missing, say so instead of inventing it. 3. Write for the stated dialect and flag any non-portable function. 4. Handle NULLs deliberately. Put conditions on the optional side of a LEFT JOIN in the ON clause, not in WHERE. Avoid NOT IN against a subquery that can return NULL; use NOT EXISTS. Choose COUNT(*) or COUNT(column) on purpose. 5. Use half-open date ranges (>= start AND < end) and state the time zone assumption. 6. Use bind parameters, not string concatenation. 7. For INSERT, UPDATE or DELETE, first give a SELECT that previews the affected rows, then the change inside a transaction with a specific WHERE clause. Never produce an unbounded UPDATE or DELETE. 8. If pagination is requested, explain whether OFFSET or keyset pagination fits the use. ## Return 1. Assumptions and the chosen interpretation 2. The query in a sql code block, with comments on non-obvious parts 3. A clause-by-clause explanation 4. A minimal test fixture (CREATE TABLE and INSERT statements, a few rows covering edge cases) and the exact rows the query should return for it 5. Index candidates that might help, labelled as hypotheses to check with the database's EXPLAIN 6. An alternative formulation only if it changes semantics or portability ## Acceptance checks - The query parses in the stated dialect and references only schema columns. - The fixture's expected rows can be derived by hand from the fixture data. - NULL, empty-group and date-boundary cases are covered by at least one fixture row each. - Write statements are previewed and guarded.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Database: SQLite 3.x Schema: CREATE TABLE customers(id INTEGER PRIMARY KEY, email TEXT NOT NULL, created_at TEXT NOT NULL); CREATE TABLE orders(id INTEGER PRIMARY KEY, customer_id INTEGER REFERENCES customers(id), total_cents INTEGER, status TEXT NOT NULL, created_at TEXT NOT NULL); Request: list customers who have not placed a paid order since a cutoff date, including customers who never ordered, with their last paid order date. Definitions: only status = 'paid' counts; refunded orders do not. Parameters: :cutoff = '2026-08-07' (90 days before 2026-11-05).
This query was executed locally in SQLite 3.54 against a 4-customer fixture; any model output remains illustrative. SELECT c.id, c.email, MAX(o.created_at) AS last_paid_order_at FROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid' GROUP BY c.id, c.email HAVING MAX(o.created_at) IS NULL OR MAX(o.created_at) < :cutoff ORDER BY c.id; The fixture has customer 1 paid 2026-10-20, customer 2 paid 2026-06-01 and refunded 2026-10-25, customer 3 paid 2026-09-10, and customer 4 with no orders. It returns (2, b@example.com, 2026-06-01) and (4, d@example.com, NULL). The refunded order does not count as recent activity, and customer 4 is kept by the LEFT JOIN.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
Models