PromptIntermediateData Analysis
SQL Query Explainer
Explain a SQL query step by step, flag performance and correctness risks, and suggest a safer rewrite.
Reviewed May 11, 2026 · ChatGPT · Claude · Cursor
Prompt workbench
Your variables
Remove connection strings and credentials.
Works with: ChatGPT, Claude, Cursor
Generated prompt
Explain this SQL query for someone who knows basic SQL but not this schema. Database: [Database] Schema context: [Schema context] Query: ```sql [SQL query] ``` Explain: 1. What the query returns, in plain English. 2. Each join and its cardinality risk. 3. Filters that may silently exclude rows (NULL handling, time zones, string comparison). 4. Aggregation and window-function behaviour. 5. Performance risks visible from the query (missing predicate, non-sargable filter, SELECT *). 6. A safer or clearer rewrite only if you can justify it. 7. Three test cases, including edge cases. Do not assume indexes exist. Do not invent table columns.
What this prompt is for
Understand inherited SQL and catch expensive or incorrect patterns before they reach production.
How to get a better result
- Include the schema and a row-count estimate for each table.
- Ask for the execution plan interpretation separately.
- Test rewrites on a read replica or sample dataset.
Example input
A LEFT JOIN from orders to refunds with a WHERE refunds.created_at > ... condition, which silently turns the join into an inner join.
Example output
The WHERE clause filters out orders with no refund, so the LEFT JOIN no longer preserves unmatched orders. If the goal is all orders with refunds in the period, move the date condition into the join predicate or use a correlated subquery. Test with an order that has no refund.
Example outputs are illustrative. Model behaviour varies; check the result before using it.
Limitations
- • The model cannot see the query plan or actual data distribution.
- • Dialect differences matter; state the database engine.
- • Generated SQL must be reviewed before running against production.