Prompts: Debug, Explain & Optimize
Prompts #3, #4, #5 and #6: speed up slow queries, read execution plans, fix errors and understand results.
When a query is slow, broken, or returns a result you don't understand, AI is a fast second pair of eyes, as long as you give it the evidence: the query, the exact error, or the execution plan.
Definition
AI-assisted debugging means giving the AI the failing query plus the exact evidence (error message, execution plan, unexpected result) and asking for the cause, the fix, and the reasoning, then verifying the fix yourself.
Asking "why" matters: a fix you understand is one you won't need to ask for again.
Prompt #3 — Optimize the performance of a query
text
The following SQL Server query is slow.
Do the following:
1. Propose optimizations to improve its performance.
2. Provide the improved SQL query.
3. Explain each improvement to understand the reasoning behind it.
[ SQL Query GOES HERE ]A typical improvement: don't wrap a filtered column in a function.
sql
-- slow: the function runs on every row, so an index on debut can't be used
WHERE YEAR(debut) = 2008
-- faster: a plain range on the column, so the index can be used
WHERE debut >= '2008-01-01'
AND debut < '2009-01-01'Both return the same three players from player_profiles:
| name | debut |
|---|---|
| Virat Kohli | 2008-04-20 |
| Rohit Sharma | 2008-04-22 |
| MS Dhoni | 2008-04-19 |
Note: Always test the new version, both that it returns the same rows and, on a real database, that its execution plan is actually better.
Prompt #4 — Optimize the execution plan
text
The image is the execution plan of a SQL Server query.
(Attach a screenshot of the plan.)
Do the following:
1. Describe the execution plan step by step.
2. Identify performance bottlenecks and issues.
3. Suggest ways to improve performance and optimize the execution plan.
[ SQL Query GOES HERE ]Why it works: the plan is evidence. The AI can point at table scans, key lookups and expensive sorts instead of guessing. You get a step-by-step explanation, the bottlenecks highlighted, and concrete fixes such as a missing index. (Execution plans are covered in the Performance chapter.)
Prompt #5 — Debugging
text
The following SQL Server query is causing this error:
[ Error Message GOES HERE ]
Do the following:
1. Explain the error message.
2. Find the root cause of the issue.
3. Suggest how to fix it.
[ SQL Query GOES HERE ]sql
SELECT name, runz FROM players;
-- SQL Server: Invalid column name 'runz'. ← typo
-- Fix:
SELECT name, runs FROM players;Tip: Paste the full error message, including the line number. "It doesn't work" gives the AI nothing to go on.
Prompt #6 — Explain the result
text
I didn't understand the result of the following SQL Server query.
Do the following:
1. Break down how SQL processes the query step by step.
2. Explain each stage and how the result is formed.
[ SQL Query GOES HERE ]What you get back: a walk through the execution order (FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY), how the final rows appear, and common mistakes to avoid. It's one of the best prompts for learning.
Common mistakes
- ❌ Trusting "this is faster" without proof. ✅ Compare row counts, then compare execution plans or timings.
- ❌ Sending only the error and not the query (or the reverse). ✅ Send both.
- ❌ Asking to "fix it" without "explain why". ✅ Ask for the root cause so you learn the pattern.
Key points to remember
- Prompt #3 (Performance): ask for optimizations, the improved query, and the reason for each change.
- Avoid functions on filtered columns, e.g. WHERE YEAR(debut) = 2008 → WHERE debut >= '2008-01-01' AND debut < '2009-01-01'.
- Prompt #4 (Execution plan): attach the plan as evidence so the AI can spot scans, key lookups and missing indexes.
- Prompt #5 (Debugging): always give the exact error message AND the query, and ask for the root cause plus the fix.
- Prompt #6 (Explain the result): ask the AI to walk through the execution order step by step.
- An optimized query must return the same rows as the original; verify before using it.
Try it yourself
Prompt #3: both filters return the same rows
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Prompt #5: read the error, then fix the typo
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: explain this result step by step
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
