The prompt
You are a database engineer. Explain and then improve the query below. **1. Plain English** Describe what the query returns, in one paragraph, as you would to someone who does not read SQL. Name the grain of the result set: one row per what? **2. Read it inside out** Walk the subqueries and CTEs in execution order, not written order. Say what each step produces and roughly how many rows. **3. Correctness concerns** - Any join that could fan out and duplicate rows - Any LEFT JOIN with a condition in WHERE that silently makes it an INNER JOIN - NULL handling in comparisons, IN lists and aggregates - Whether DISTINCT is hiding a join problem rather than solving one **4. Performance** For each predicate, say whether an index could serve it and why it might not be used — leading column order, a function wrapped around the column, implicit type casts, low selectivity. **5. Proposed changes** Give the rewritten query and the CREATE INDEX statements. For each change, state what it costs: index write overhead, storage, or a behaviour difference the application must handle. Flag anything that changes the result set rather than just the plan. Do not suggest an index on every column in the WHERE clause. --- Database engine and version: [e.g. PostgreSQL 16] Approximate table sizes: [e.g. orders 40M rows, customers 2M] Query: [PASTE YOUR SQL HERE]
Replace everything in [SQUARE BRACKETS] before you run it. The bracketed fields are not optional — they are what the output is built from.
Why this prompt is built this way
This prompt separates two jobs that usually get muddled: understanding what a query does, and making it faster. The order matters, because an optimisation applied to a query you have misread is how a report quietly starts returning wrong numbers.
Section 1 asks for the grain of the result set — one row per what? That is the question that resolves most confusion about an unfamiliar query, and it is the first thing an experienced engineer asks. Section 2, reading inside out in execution rather than written order, matches how the database actually evaluates it.
Section 3 is a checklist of specific traps rather than a general request to look for bugs. The LEFT JOIN item is the standout: a condition on the right-hand table placed in WHERE instead of ON silently converts it to an INNER JOIN and drops rows, and it is nearly invisible on reading. The DISTINCT item is similarly pointed — DISTINCT is very often a plaster over a join that fans out, and deduplicating treats the symptom while the cause keeps producing extra rows.
The two closing constraints — flag anything that changes the result set, and do not suggest an index on every WHERE column — exist because those are the two standard failure modes of automated SQL advice.
What to use it for
- Inheriting a slow report query
- Reviewing a migration
- Understanding legacy SQL
- Index planning
Adapting it to your situation
- Give the engine and version. PostgreSQL, MySQL and SQL Server differ enough on planner behaviour and index capability that generic advice is often actively wrong.
- Give approximate row counts. Index recommendations without table sizes are guesses; selectivity is the whole question.
- Paste the EXPLAIN output if you have it. This changes the answer more than anything else you can supply, because it replaces speculation about the plan with the actual plan.
- Mention existing indexes so it does not propose ones you already have.
Where this goes wrong
Never create a proposed index on production without testing it first. Indexes cost write throughput and storage, and on a large table the creation itself can lock or saturate IO.
Verify that a rewritten query returns identical results before deploying it, ideally by running both and comparing. A faster query with different semantics is not an optimisation.
Which model to run it on
Claude and ChatGPT both reason well about SQL. Neither can see your statistics, your hardware or your real plan, so everything either produces is a hypothesis for you to measure.
Practical tips
Give the engine and the row counts. Advice that is right for PostgreSQL is often wrong for MySQL, and an index recommendation without table sizes is guesswork. Paste the actual EXPLAIN output if you have it — it changes the answer substantially.