SQL Query Explainer
By Minh Nguyen · Published · Updated
Walks through a SQL query step by step in plain language, then points out where the logic could give wrong numbers or the plan could be slow.
Prompt template
## SQL Query Explainer Explain this SQL query. - Query: [textarea: Paste the SQL] - Database: [dropdown: PostgreSQL, MySQL, SQL Server, BigQuery, SQLite, Snowflake] - Table descriptions: [textarea: Key tables with columns and rough row counts] - Reader's SQL level: [dropdown: Beginner, Comfortable, Advanced] - What the result is used for: [text: A weekly revenue report] - Known problem: [textarea: Wrong totals or slow or none] Start with one sentence on what the query returns and for whom. Then walk through it in logical execution order rather than written order: FROM and joins, WHERE, GROUP BY, HAVING, window functions, SELECT, ORDER BY and LIMIT, explaining each clause and the rows it keeps or removes. Describe each join with its type, its key and whether it could multiply rows when the key is not unique. Pitch the language to the reader's level and define terms for beginners. Then look for correctness traps: join fan-out inflating sums, NULL handling in comparisons and counts, a filter on the outer table that silently turns a left join into an inner join, time zone and date boundaries, and integer division. Look for speed traps: functions on indexed columns, leading wildcards, correlated subqueries, missing limits, and large sorts. Finish with a short list of suggested rewrites and the check queries that would confirm the totals.