SQL Query Explainer Skill
This skill explains SQL queries in plain language, identifies optimisation opportunities, and helps communicate data logic to non-technical stakeholders. It also writes and documents new queries from natural language descriptions.
Required Inputs
- The SQL (Explain/Optimise/Document modes) — the actual query, ideally with the dialect named (Postgres, BigQuery, Snowflake, MySQL…); dialect changes both semantics and the optimisation advice.
- The intent in plain words (Write mode) — what question the data should answer, plus table/column names if known. Without a schema, assumptions get stated, never silently invented.
- Optional but transformative:
EXPLAIN/EXPLAIN ANALYZE output and rough table sizes — turns generic advice into advice about your query plan.
Modes
Detect which mode the user needs based on their request:
- Explain — Translate existing SQL into plain English
- Optimise — Review SQL for performance issues and suggest improvements
- Write — Generate SQL from a natural language description
- Document — Produce a data dictionary or query documentation
Mode 1: Explain
When given a SQL query, produce:
Plain English Summary
[1–3 sentences. What does this query do? What data does it return? Write as if explaining to a business analyst, not a developer.]
Step-by-Step Walkthrough
Break the query into logical sections. For each section:
- Quote the SQL clause
- Explain what it does in plain English
- Flag any complexity (e.g. window functions, subqueries, CTEs)
What the Result Looks Like
[Describe the shape of the output: "Returns one row per user, with columns for X, Y, Z. Ordered by [field] descending."]
Potential Issues to Flag
- [Gotchas, edge cases, or implicit assumptions in this query]
- [e.g. "This will include NULLs in the user_id column if the LEFT JOIN finds no match"]
Mode 2: Optimise
When asked to optimise a query, produce:
Performance Assessment
Rate overall: 🟢 Well-optimised / 🟡 Some improvements possible / 🔴 Significant issues
Issues Found
For each issue:
Issue [N]: [Short name, e.g. "Missing index on join column"]
- What it is: [Plain explanation]
- Why it matters: [Performance impact — e.g. "Full table scan on a 10M row table"]
- Fix:
-- Before
[original snippet]
-- After
[improved snippet]
- Expected improvement: [Estimate if possible]
Optimisation Checklist
Mode 3: Write
When given a natural language description, generate the SQL query and then explain it using Mode 1.
Ask the user to confirm:
- Database/dialect (PostgreSQL / MySQL / BigQuery / Snowflake / SQLite / Standard SQL)
- Table and column names (if known; otherwise use descriptive placeholder names like
users, orders, user_id)
- Any filters, sorting, or aggregation requirements
Produce:
- The SQL query with inline comments
- Plain English explanation (Mode 1 format)
Mode 4: Document
When asked to create documentation for a query or table:
Query Documentation
Query: [Name]
Purpose: [One sentence — what business question this answers]
Author: [If provided]
Last reviewed: [If provided]
Inputs:
- Table: [table_name] — [what it contains]
- Filter: [any WHERE conditions and their business meaning]
Output columns:
| Column | Type | Description |
|--------|------|-------------|
| [name] | [type] | [plain English description] |
Assumptions:
- [Any implicit assumptions the query makes]
Known limitations:
- [Edge cases not handled, data quality dependencies, etc.]
Output Format
Every mode returns the same disciplined shape:
- The one-line summary — what this query does, in business language ("monthly revenue per region, excluding refunds"), before any SQL talk.
- The walkthrough or the artifact — mode-dependent: annotated clause-by-clause explanation (Explain), the rewritten query with a diff of what changed and why (Optimise), the new query with stated assumptions (Write), or the doc block (Document).
- The gotchas — NULL behaviour, join fan-out, timezone traps, and index implications that apply to this query, not generic advice.
- Verification — a small
SELECT the user can run to confirm the query does what the summary claims (row counts before/after, a spot-check predicate).
Quality Checks
Anti-Patterns
- Restating the SQL in pseudo-code instead of explaining what it does and returns
- Optimisation advice with no before/after query, or no reason the new one is faster
- Ignoring the dialect (writing Postgres-only syntax for a MySQL user)
- "Looks fine" with no read on correctness, performance, or row grain
- Rewriting the query from scratch instead of explaining/optimising the user's
Example Trigger Phrases
- "Explain this SQL query: [paste query]"
- "Optimise this slow query: [paste query]"
- "Write a SQL query that [natural language description]"
- "Document this query for my non-technical stakeholders"
- "Why is this query returning unexpected results?"
1---2name: sql-query-explainer3description: Explains, optimises, writes, and documents SQL queries. Use when asked to explain a SQL query, optimise slow SQL, translate SQL to plain English for non-technical stakeholders, write a query from a natural language description, or produce query documentation. Produces plain-English explanations, annotated optimised queries, or a data dictionary covering output shape, assumptions, and known limitations. Works across PostgreSQL, MySQL, BigQuery, Snowflake, and standard SQL.4---5
6# SQL Query Explainer Skill
7
8This skill explains SQL queries in plain language, identifies optimisation opportunities, and helps communicate data logic to non-technical stakeholders. It also writes and documents new queries from natural language descriptions.
9
10## Required Inputs
11
12- **The SQL** (Explain/Optimise/Document modes) — the actual query, ideally with the dialect named (Postgres, BigQuery, Snowflake, MySQL…); dialect changes both semantics and the optimisation advice.
13- **The intent in plain words** (Write mode) — what question the data should answer, plus table/column names if known. Without a schema, assumptions get stated, never silently invented.
14- Optional but transformative: `EXPLAIN`/`EXPLAIN ANALYZE` output and rough table sizes — turns generic advice into advice about *your* query plan.
15
16## Modes
17
18Detect which mode the user needs based on their request:
19
201. **Explain** — Translate existing SQL into plain English
212. **Optimise** — Review SQL for performance issues and suggest improvements
223. **Write** — Generate SQL from a natural language description
234. **Document** — Produce a data dictionary or query documentation
24
25---
26
27## Mode 1: Explain
28
29When given a SQL query, produce:
30
31### Plain English Summary
32[1–3 sentences. What does this query do? What data does it return? Write as if explaining to a business analyst, not a developer.]
33
34### Step-by-Step Walkthrough
35
36Break the query into logical sections. For each section:
37- Quote the SQL clause
38- Explain what it does in plain English
39- Flag any complexity (e.g. window functions, subqueries, CTEs)
40
41### What the Result Looks Like
42
43[Describe the shape of the output: "Returns one row per user, with columns for X, Y, Z. Ordered by [field] descending."]
44
45### Potential Issues to Flag
46
47- [Gotchas, edge cases, or implicit assumptions in this query]
48- [e.g. "This will include NULLs in the user_id column if the LEFT JOIN finds no match"]
49
50---
51
52## Mode 2: Optimise
53
54When asked to optimise a query, produce:
55
56### Performance Assessment
57
58Rate overall: 🟢 Well-optimised / 🟡 Some improvements possible / 🔴 Significant issues
59
60### Issues Found
61
62For each issue:
63
64**Issue [N]: [Short name, e.g. "Missing index on join column"]**
65- **What it is:** [Plain explanation]
66- **Why it matters:** [Performance impact — e.g. "Full table scan on a 10M row table"]
67- **Fix:**
68```sql
69-- Before
70[original snippet]
71
72-- After
73[improved snippet]
74```
75- **Expected improvement:** [Estimate if possible]
76
77### Optimisation Checklist
78
79- [ ] SELECT * used? (Replace with specific columns)
80- [ ] Implicit type conversions on JOIN/WHERE columns?
81- [ ] Missing indexes on JOIN or WHERE columns?
82- [ ] N+1 patterns (queries inside loops)?
83- [ ] DISTINCT used where GROUP BY would be faster?
84- [ ] Window functions used where a subquery would be clearer/faster?
85- [ ] CTEs re-used or materialised unnecessarily?
86- [ ] Large IN() lists that could use a JOIN instead?
87
88---
89
90## Mode 3: Write
91
92When given a natural language description, generate the SQL query and then explain it using Mode 1.
93
94Ask the user to confirm:
95- **Database/dialect** (PostgreSQL / MySQL / BigQuery / Snowflake / SQLite / Standard SQL)
96- **Table and column names** (if known; otherwise use descriptive placeholder names like `users`, `orders`, `user_id`)
97- **Any filters, sorting, or aggregation requirements**
98
99Produce:
1001. The SQL query with inline comments
1012. Plain English explanation (Mode 1 format)
102
103---
104
105## Mode 4: Document
106
107When asked to create documentation for a query or table:
108
109### Query Documentation
110
111```
112Query: [Name]
113Purpose: [One sentence — what business question this answers]
114Author: [If provided]
115Last reviewed: [If provided]
116
117Inputs:
118 - Table: [table_name] — [what it contains]
119 - Filter: [any WHERE conditions and their business meaning]
120
121Output columns:
122 | Column | Type | Description |
123 |--------|------|-------------|
124 | [name] | [type] | [plain English description] |
125
126Assumptions:
127 - [Any implicit assumptions the query makes]
128
129Known limitations:
130 - [Edge cases not handled, data quality dependencies, etc.]
131```
132
133---
134
135## Output Format
136
137Every mode returns the same disciplined shape:
138
1391. **The one-line summary** — what this query does, in business language ("monthly revenue per region, excluding refunds"), before any SQL talk.
1402. **The walkthrough or the artifact** — mode-dependent: annotated clause-by-clause explanation (Explain), the rewritten query with a diff of what changed and why (Optimise), the new query with stated assumptions (Write), or the doc block (Document).
1413. **The gotchas** — NULL behaviour, join fan-out, timezone traps, and index implications that apply to *this* query, not generic advice.
1424. **Verification** — a small `SELECT` the user can run to confirm the query does what the summary claims (row counts before/after, a spot-check predicate).
143
144## Quality Checks
145
146- [ ] Plain English explanation avoids SQL jargon
147- [ ] Optimisation suggestions include before/after SQL
148- [ ] Written queries include inline comments
149- [ ] Output shape is described (columns, row grain, ordering)
150- [ ] Dialect-specific syntax is flagged when non-standard
151
152## Anti-Patterns
153
154- Restating the SQL in pseudo-code instead of explaining what it *does* and *returns*
155- Optimisation advice with no before/after query, or no reason the new one is faster
156- Ignoring the dialect (writing Postgres-only syntax for a MySQL user)
157- "Looks fine" with no read on correctness, performance, or row grain
158- Rewriting the query from scratch instead of explaining/optimising the user's
159
160## Example Trigger Phrases
161
162- "Explain this SQL query: [paste query]"
163- "Optimise this slow query: [paste query]"
164- "Write a SQL query that [natural language description]"
165- "Document this query for my non-technical stakeholders"
166- "Why is this query returning unexpected results?"