Explain a Complex SQL Query in Plain English, Step by Step

Get a plain-English walkthrough of any SQL query in execution order, with tiny sample tables, row counts after each clause, and the hidden gotchas.

You getSummary, sample data and step-by-step table with columns

Prompt

Prompt
You are a senior data engineer who teaches SQL to analysts.

I will paste a SQL query right after this message. Explain what it does in plain English, step by step, in the order the database actually runs it: FROM, JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT. Skip clauses the query does not use.

Rules:
1. Start with a one-sentence summary a non-technical manager would understand.
2. Build a tiny sample dataset (3-6 rows per table) and show it first. For every step, state the row count after that clause using this data.
3. Define every alias, window function, CTE or subquery the first time it appears.
4. Flag anything that can silently change results: NULLs in join keys or filters, duplicate rows from joins, a WHERE condition that turns a LEFT JOIN into an INNER JOIN, integer division.
5. Do not rewrite the query. You may suggest at most two improvements at the end.

Output format:
1. Summary (one sentence)
2. Sample data (small Markdown tables)
3. Step-by-step table with columns: Step | Clause | What happens | Rows after
4. Final result (Markdown table from the sample data)
5. Gotchas and suggestions (bullets)

If a table or column's meaning is unclear, state your assumption in one line instead of guessing silently.

Example output

At a glanceRows remaining after each SQL clauseSample data flowing through the query in execution order
  • FROM customers: 4
  • LEFT JOIN orders: 6
  • WHERE paid: 4
  • GROUP BY region: 2 groups
  • HAVING 2+ orders: 2
  • SELECT: 2
  • ORDER BY revenue: 2

Example for: an analyst reviewing a regional revenue query that uses a LEFT JOIN, GROUP BY and HAVING.

SELECT c.region, COUNT(o.id) AS orders, SUM(o.amount) AS revenue
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
GROUP BY c.region
HAVING COUNT(o.id) >= 2
ORDER BY revenue DESC;
1. Summary

For each region with at least two paid orders, this shows how many paid orders there were and their total value, highest revenue first.

2. Sample data

customers (c)

idnameregion
1AnaNorth
2BenNorth
3CleoSouth
4DevWest

orders (o)

idcustomer_idamountstatus
101150paid
102130refunded
103220paid
104380paid
105340paid
3. Step by step
StepClauseWhat happensRows after
1FROM customers cStarts with every customer; c is a short alias.4
2LEFT JOIN orders oAttaches each customer's orders. Dev has none, so he gets one row with NULL order columns.6
3WHERE o.status = 'paid'Keeps only paid orders. Drops refund 102 and Dev's NULL row.4
4GROUP BY c.regionCollapses rows into one group per region: North (101, 103), South (104, 105).2 groups
5HAVING COUNT(o.id) >= 2Keeps groups with 2+ orders. Both pass.2
6SELECTOutputs region, order count and summed amount.2
7ORDER BY revenue DESCSorts by the revenue alias, largest first.2
4. Final result
regionordersrevenue
South2120
North270
5. Gotchas and suggestions
  • The LEFT JOIN acts like an INNER JOIN. WHERE o.status = 'paid' removes rows where o.status is NULL, so customers with no orders (Dev, West) disappear. If you meant to keep them, move the filter into the join: ON o.customer_id = c.id AND o.status = 'paid'.
  • NULL revenue: with that fix, West would show orders = 0 but revenue = NULL. Wrap it as COALESCE(SUM(o.amount), 0) if reports need a zero.
  • Assumption: status values are lowercase; if not, 'Paid' rows would be silently excluded.
Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Following real execution order (FROM before SELECT) shows why aliases and filters behave as they do, which reading top to bottom hides.
  • Row counts on tiny sample data make join duplication and dropped rows visible instead of abstract.
  • A required gotchas list catches silent bugs like a WHERE filter that cancels a LEFT JOIN.

Use it when you inherit a report query, review a pull request, or debug odd numbers. Paste the full query right after the prompt in ChatGPT, Claude or Gemini; add table notes if column names are cryptic.

Common edits

  • Name your database (PostgreSQL, MySQL, BigQuery, SQL Server) so the explanation covers dialect-specific functions.
  • Allow a full rewrite at the end by changing rule 5 to ask for a cleaner, commented version.

Prompt details

By
Prompts-GPT editors
Published

Did this prompt work?

Keep exploring

More in Coding