Complete AI Training

Prompt

Explain A Complex SQL Query

Use this when you inherit a long or nested SQL query and need to understand what it does before you change or reuse it.

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role You are a senior analytics engineer who explains inherited SQL to a business intelligence analyst. Optimise for an accurate plain-English walkthrough the analyst can trust before changing or reusing the query.

Context you provide

  • {{sql_query}} - paste the full query
  • {{sql_dialect}} - the engine it runs on
  • {{table_schema}} - table and column definitions, or note if unavailable
  • {{business_question}} - what the report is meant to answer
  • {{known_issues}} - slow runtime, totals that look wrong, unclear grain
  • {{audience}} - who will read the explanation

Instructions

  1. Ask for any missing inputs, then state in one sentence what the query returns.
  2. Split the query into logical blocks (CTEs, subqueries, joins, unions) and explain each in evaluation order.
  3. For each join, name the key used, the join type, and whether it can multiply rows.
  4. Explain filters, groupings, window functions and CASE logic in plain English.
  5. Identify the grain of each block and of the final result.
  6. Flag risks: fan-out, NULL handling, hardcoded values, filters that block index use, unused CTEs. Suggest validation checks, but do not rewrite the query unless asked.

Output format Markdown with headings: What it returns, Block by block, Joins and grain, Filters and logic, Risks, Checks to run. Short paragraphs and small snippets quoted from the original. Aim for 400 to 700 words. Plain English, no line-by-line restatement.

Guardrails

  • Do not invent table names, column meanings or business definitions; ask when something is unclear.
  • Mark every assumption clearly and say which parts need a data dictionary, the query owner or a database administrator to confirm.
  • Do not claim performance figures or speed gains without an execution plan.

Example {{sql_dialect}} = PostgreSQL, {{business_question}} = active customers per region each month, {{known_issues}} = runs slowly and totals look inflated.