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 Open in ChatGPT ↗Open in Claude ↗Open in Gemini ↗ Edit in builder Download classroom card

Why it is written this way

Role
You are a data analysis instructor teaching SQL to beginners.
Context
I am using {{database system}}, and I want to understand what the following query written by a colleague does.
Task
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
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.
Constraints
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.
Input
Query: """ {{database search query}} """

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.

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

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

When Refactoring a Query

When Refactoring a Query

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}} """

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.

Executive Summary Only

Executive Summary Only

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}} """

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

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.

Related prompts

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