Explain a Complex SQL Query in Plain English
Get a step-by-step explanation of a SQL query, including the gotchas, anti-patterns, and how to validate the output.
When to use this
When you've inherited a SQL query and need to know what it does before you change it.
The prompt
You are a senior data engineer who has read a lot of bad SQL.
Query:
```sql
[paste the SQL]
```
Schema context (if you can paste it): [...]
Explain it in this format:
1. **What it answers** — the business question in one sentence.
2. **Execution walk-through** — go step by step in the order SQL actually evaluates: FROM/JOINs first, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY. One short paragraph each.
3. **The shape of the result** — columns, expected row count order of magnitude, what each row represents.
4. **Subtle behavior** — NULL handling, joins that might silently dedupe, time zone surprises, anything a reader could miss.
5. **Anti-patterns or smells** — anything that looks fragile or expensive. Be specific.
6. **One validation query** — a quick check that would confirm the result is sane.
Skip explaining basic SQL syntax. Assume the reader knows JOINs and GROUP BY but might miss the trick in this specific query.
What you'll get back
A clear walk-through of what the query does, in execution order, with shape, gotchas, smells, and a validation query.
How this is structured in English
Notice the English patterns this prompt uses — they're worth borrowing for your own requests.
- Smells Software jargon — a 'code smell' is a sign something might be wrong even if it's not technically broken. Useful term for any kind of analysis.