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
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
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 like this
Write SQL Window Function Queries for Running Totals, Ranks and Period Changes
Get a commented SQL window function query for running totals, rankings or period comparisons, with each PARTITION BY, ORDER BY, tie and NULL choice explained.
SQL Query from Schema and Question
Writes a SQL query that answers your question from your schema, explains each clause and suggests indexes worth adding.
Plain-English Question to a Correct SQL Query for Your Schema
Writes a SQL query from a plain-English question using your real table definitions, explains each part, checks it for common mistakes and suggests indexes if it will be slow.
Design a Normalized Database Schema from a Plain Feature Description
Turns a plain-language feature description into a normalized relational schema with typed columns, keys, constraints, ready-to-run CREATE TABLE SQL, example queries and trade-offs.
Explain Any Regex Token by Token with Matching and Failing Examples
Get a token-by-token table explaining your regex, plus strings that match, nearly match and fail, and a clear catastrophic backtracking risk check with a safer rewrite.
Find and Fix Data Quality Issues with Ready-to-Run SQL Checks
Paste your schema and get SQL queries that catch duplicates, NULLs, orphaned foreign keys and out-of-range values, each with what a bad result means and a fix.