Explain what a SQL query returns clause by clause, read a supplied execution plan in plain language, and propose only rewrites proven to return the same rows.
Explain the SQL query below so a developer who did not write it understands exactly what it returns and why. If an execution plan is supplied, translate it into plain language. Propose rewrites only when they are semantically equivalent or when the difference is stated. Index and plan tuning experiments belong in a separate database review.
## Inputs
SQL query: [QUERY]
Database system and version: [DATABASE]
Relevant schema: columns, nullability, keys and indexes: [SCHEMA]
Execution plan output (EXPLAIN or EXPLAIN ANALYZE), if available: [PLAN]
What the query is supposed to return: [INTENT]
## Instructions
1. **Parse the query**: break it into SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT and subqueries, and describe them in logical evaluation order.
2. **Explain the result**: the grain of the output (one row per what), and how joins can duplicate or drop rows.
3. **Find correctness pitfalls**: NULL behaviour (for example `NOT IN` with a nullable subquery returns no rows), outer joins turned into inner joins by WHERE filters, implicit type casts, non-deterministic ordering, and mismatches with [INTENT].
4. **Analyze the plan** if [PLAN] is supplied: scan types, join methods, estimated versus actual rows, and the most expensive steps. If no plan is supplied, set `execution_plan` to null and say which EXPLAIN command to run; warn that EXPLAIN ANALYZE executes the statement.
5. **Recommend**: rewrites with the reason and an equivalence argument; statistics updates or index candidates only as hypotheses for plan review.
6. Do not present a rewrite as faster when it is logically identical to the original; for example, moving a filter from WHERE into an INNER JOIN's ON clause does not change results and usually not the plan.
## Return
JSON with:
- `original_query`
- `explanation`: ordered steps in plain language, including the output grain
- `pitfalls`: issues found, with the affected clause
- `execution_plan`: plain-language reading of [PLAN], or null
- `optimization_recommendations`: list of {type, component, description, equivalence, confidence}
- `optimized_query`: the rewrite, or null if no change is justified
- `equivalence_checks`: SQL to compare old and new results (for example EXCEPT in both directions on test data)
## Acceptance checks
- The explanation states the output grain.
- Every rewrite has an equivalence argument and an executable comparison.
- Plan statements come only from [PLAN].
- No timing improvement is claimed.Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Query: SELECT c.customer_id, c.email FROM customers c WHERE c.customer_id NOT IN (SELECT o.customer_id FROM orders o WHERE o.order_date > '2025-01-01'); Database: PostgreSQL 17 Schema: customers(customer_id PK, email); orders(order_id PK, customer_id NULL allowed for guest checkouts, order_date date) Plan: not supplied Intent: customers who have not ordered since 2025-01-01
JSON whose explanation says the output is one row per customer; whose pitfalls entry states that because orders.customer_id is nullable, a single guest order after the date makes NOT IN return no rows at all; whose optimized_query uses NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id AND o.order_date > '2025-01-01') with the difference explained as a correctness fix, not a speedup; execution_plan null with the EXPLAIN command to run; and equivalence_checks that show the two queries agree when no NULL customer_id exists and differ when one does. Illustrative expectation; the NOT IN/NOT EXISTS NULL behaviour and the original page's WHERE-versus-ON example were executed locally in SQLite (see report).
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: