
- Updated
Understand a long SQL query step by step
SQL runs in a different order from how it is written. This prompt explains your query step by step in the real order, shows tiny example tables as the data changes and points out join and NULL risks.
Act as a SQL teacher. Query: [PASTE THE SQL] Database: [e.g. PostgreSQL] Tables involved (optional): [PASTE SCHEMAS] Explain the query in the order the database actually runs it (FROM, JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY), not the order it is written. Describe what the data looks like after each step with a tiny example table. Then point out any bug risk, like duplicate rows from a join or NULLs being dropped.
Ask for the tiny example tables. Seeing the rows change at each step is what makes SQL click.
Get a better result
Your SQL explained in the order the database runs it, from FROM and JOIN through to ORDER BY, with a tiny example table after each step and warnings about duplicate rows, dropped NULLs and similar traps.
SQL is written in one order and run in another, and that gap is why long queries feel confusing. Walking through the run order, with tiny tables after each step, lets you watch rows appear and disappear, as the tip says. Asking for bug risks at the end catches the classic problems, like a join that doubles your totals.
The example tables do not match your real columns.
Fix: Paste the table schemas, even just the column names, so the sample rows use your real fields.
- Window functions and subqueries are skipped. Ask it to explain each subquery first as its own small query, and to say at which step any window function runs.
- It says the query is fine when the totals look wrong. Tell it the number you expected and the number you got, so it can trace which join or filter changed the rows.
Ask for three small questions about this query, like “what happens if a customer has no orders?”, and answer before it explains.
Rewrite the query with a short comment above each part, and fix any duplicate rows or dropped NULLs you pointed out.
More chat & writing prompts
See all 465 →More like this
- Review a database schema before it goes liveClaude · ChatGPT · Gemini
- Design a database schemaChatGPT · Gemini · Claude
- Clean a messy spreadsheet with PythonClaude · ChatGPT · Gemini
- Speed up a slow API endpoint or functionClaude · ChatGPT · Gemini
- Learn a programming concept with analogiesClaude · ChatGPT · Gemini



