thinkhard.SI

Prompt repository Data & spreadsheets

Explain a SQL query before you reuse or change it

Someone gave you a query that produces a report, and you need to know what it does, what it counts and how to change a date or filter safely.

Registry no.
DAT-027
Collection
Data & spreadsheets
Laid out for
Any chat model
Score
10 out of 10
Blanks to fill
6
Added

Data & spreadsheetsAny chat

The prompt

Score 10 of 10, Excellent

Use it when Someone gave you a query that produces a report, and you need to know what it does, what it counts and how to change a date or filter safely.

  • {{database}}
  • {{stated_purpose}}
  • {{change_wanted}}
  • {{sql_level}}
  • {{query}}
  • {{schema_notes}}
Full prompt and why it works
Prompt
Database or tool it runs in: {{database}}
What I was told the query does: {{stated_purpose}}
What I want to change, if anything: {{change_wanted}}
My SQL level: {{sql_level}}

The query:
"""
{{query}}
"""

Notes on the tables and columns, if I have them (what each table holds, what one row is):
"""
{{schema_notes}}
"""

Task:
Explain the query above in plain words, so that I can reuse it with confidence and make the change I described without breaking it.

Format:
1. In one paragraph: what one row of the result is, what each column means, and which records are included and left out.
2. Part by part: walk through the query in the order the database runs it (FROM and JOINs, WHERE, GROUP BY, HAVING, SELECT, ORDER BY), quoting each piece and explaining it in one or two sentences.
3. Things that could make the numbers wrong: for example a join that can repeat rows and double count, a filter that drops rows with blank values, a hard-coded date, a time zone, or COUNT(*) where COUNT(DISTINCT ...) was meant. For each, say whether it applies here and how to check, such as counting rows before and after a join.
4. My change: the edited query in a code block, with each changed line marked by a comment, and anything else that might need to change with it.
5. A quick test: one simple query or check to confirm the edited version returns what I expect.

Requirements:
- Under 500 words, plus code.
- Do not assume what a table or column holds beyond its name and my notes; when you must, say it, for example "assuming orders has one row per order".
- Keep the {{database}} dialect, and point out any function specific to it.
- Change nothing I did not ask to change.

Example of one part-by-part line's form only: "LEFT JOIN refunds r ON r.order_id = o.id: keeps every order and adds refund details where they exist; an order with two refunds appears twice."

If what I was told the query does does not match what it actually does, say so first, plainly.

Variables

{{database}}
For example "PostgreSQL", "SQL Server", "BigQuery" or "Snowflake". Write "not sure" if you do not know.
{{stated_purpose}}
What the person who gave it to you said, for example "monthly active customers by region".
{{change_wanted}}
For example "run it for last quarter instead of last month". Write "none, just explain" if no change.
{{sql_level}}
For example "I can read SELECT and WHERE, joins confuse me".
{{query}}
The full query, copied exactly.
{{schema_notes}}
Anything you know about the tables. Write "none" if you have nothing.

Why it works

  • What good looks like. It walks the query in the order the database runs it and names the joins and filters that quietly double count or drop rows, so you know what the numbers mean.
  • What to avoid. The edit changes only what you asked for, with changed lines marked, so a reused report does not drift in ways nobody notices.
  • If unsure. Anything it assumes about a table is stated in words, and a mismatch between what you were told and what the query does is raised first.

How to use it

  1. Press Fill in variables and type your details into each blank. What you type stays in this browser.
  2. Copy the filled prompt into the AI tool you already use, such as ChatGPT, Claude or Gemini, or open it in the Grader or the Prompt Tester here.
  3. Before you rely on it, try it on three or four real examples. The Prompt Tester runs them all at once and shows which ones go wrong.
Use it from code

Get this prompt as JSON from the free REST API, with your own text in the blanks, or let an AI assistant fetch it through the MCP server. No key.

curl -s https://thinkhard.si/api/v1/prompts/explain-sql-query

With the MCP server added to Claude, ChatGPT, Cursor or VS Code, call get_prompt with {"prompt": "explain-sql-query"}, or just ask for it by name.