Coding & data

Clause-by-Clause SQL Query Explanation

Explains complex SQL queries step-by-step in logical order of execution.

Prompt · 2 variables

You are a data analysis instructor teaching SQL to beginners.

I am using {{database system}}, and I want to understand what the following query written by a colleague does.

Please break down the query clause by clause and explain what each part does. Base your explanation on the logical execution order (FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY).

Format: 1. Start with a one-sentence summary: "In short, this query..." 2. Provide a table for each clause: "Code Snippet | What it does | Points to note" 3. Conclude with a 3-row sample table showing what the final output would look like.

When introducing technical terms, add a brief, simple explanation in parentheses the first time they appear. If you notice any obvious performance bottlenecks or common pitfalls in the query, point out just 1 or 2 at the very end.

Query: """ {{database search query}} """

Copy, then paste here · ChatGPT and Claude open with the prompt filled in

Why it is written this way

This prompt is built from 6 blocks
Role Context +1 Task Format Constraints Input +1 Role Material Work Output Material

Tap a block to see its paragraph

Role Role · Who answers

You are a data analysis instructor teaching SQL to beginners.

See the Assign a role technique
  • Role Who answers ×1
  • Material What the AI needs to know ×2
  • Work What to do, and how ×1
  • Output What comes back, in what shape ×2

Asking an AI to explain code is one of its strongest capabilities, but without setting the specific "order" and "depth", responses tend to be inconsistent. This prompt standardizes both.

Read the full explanation · 3 paragraphs

Setting the Role to an "instructor teaching beginners" sets the ideal depth for the explanation. Asking a "Senior DBA" for the same explanation would immediately dive into query plans and index scans. Specifying the dialect in the Context is critical because syntax varies across dialects—LIMIT exists in MySQL and PostgreSQL while SQL Server uses TOP, and date functions vary widely. This single variable prevents unnecessary or irrelevant syntax corrections.

In the Task paragraph, emphasizing "execution order" is key. SQL is written in one order (SELECT first) but executed in another (FROM first), which is where beginners often get confused. Grounding the explanation in execution order clarifies questions like "Why can't I use column aliases in WHERE clauses?"

In the Format section, the "In short..." sentence provides high-level context first, while the "3-row sample table" helps visualize the concrete end result. Without these, even accurate explanations leave users asking, "So what does the final output actually look like?" The Constraint limiting feedback to "just 1 or 2" prevents the model from generating lengthy, nitpicky style advice. Wrapping the query in """ ensures comments inside the query (-- remove this later) are not misinterpreted as instructions.

Unfamiliar terms? See Aha AI: prompt, hallucination

Compared with a bad example

Missing from the bad prompt: Role · Context · Format · Constraints
Bad prompt 37 chars This prompt 720 chars Role Context Task Format Constraints Input Role · missing Context · missing Format · missing Constraints · missing
Common bad example

Explain this query SELECT ... (paste query)

While you will get an explanation, it typically skims down the query in written order starting from SELECT, missing key beginner questions like "When is the data actually filtered and grouped?" Because no specific DB is mentioned, irrelevant syntax warnings may also appear.

Variations

1

When Refactoring a Query

Switches the role to an engineer and explicitly states "Instead of explaining". Adding "Do not suggest speculative optimizations" prevents unverified suggestions or risky query rewrites.

Show prompt

You are a data engineer experienced in {{database system}} performance tuning. Instead of explaining the query below, rewrite it to be more readable or faster while returning the exact same results. Structure your response as follows: Rewritten Query → Comparison Table (Before · After · Reason) → Warnings if any edge cases might produce different results. Do not suggest speculative optimizations you are not confident in.

""" {{database search query}} """

2

Executive Summary Only

Useful when communicating the purpose of a query to product managers, business analysts, or team leads.

Show prompt

Summarize what the following {{database system}} query does in two simple sentences as if explaining to a non-technical stakeholder. Do not use technical jargon.

""" {{database search query}} """

Model notes

If table schemas are not provided, the AI will infer the meaning of column names. Including CREATE TABLE statements or column lists will significantly improve accuracy.

Last updated 2026-09-02 · Found a mistake? Let us know