AI Prompts for SQL
A collection of 16 prompt templates for using AI tools (ChatGPT, Copilot, Claude) to accelerate SQL development and learning.
1. Solve an SQL Task
In my SQL Server database, we have two tables:
orders(order_id, sales, customer_id, product_id) andcustomers(customer_id, first_name, last_name, country). Write a query to rank customers based on their sales. Write three different versions and compare readability and performance.
2. Improve Readability
The following SQL Server query is long and hard to understand. Improve its readability, remove redundancy, and explain each change.
3. Optimize Performance
The following SQL Server query is slow. Propose optimizations, provide the improved query, and explain each improvement.
4. Analyze Execution Plans
This image shows the execution plan. Describe it step by step, identify bottlenecks, and suggest optimizations.
5. Debug Errors
The query causes error: "Msg 8120, Level 16, State 1". Explain the error, find the root cause, and suggest how to fix it.
6. Explain Results
I don't understand the result of this query. Break down how SQL processes it step by step.
7. Style & Format Code
Restyle this SQL code: align aliases, keep it compact, follow best practices.
8. Documentation & Comments
This query lacks documentation. Add a leading comment, inline comments where needed, and create a business rules document.
9. Improve Database DDL
Review this DDL for naming consistency, data type optimization, integrity constraints, index coverage, and normalization.
10. Generate Test Data
Generate realistic INSERT statements for this DDL. Keep the dataset small, ensure FK relationships are valid, and avoid NULLs.
11. Create a SQL Course
Create a comprehensive SQL course roadmap: start with fundamentals, advance to complex topics, focus on data analytics use cases.
12. Understand a Concept
Explain SQL Window Functions: what they are, an analogy, when to use them, syntax, examples, and top 3 use cases.
13. Compare Concepts
Compare Window Functions vs GROUP BY: key differences, when to use each, pros/cons, and a side-by-side summary table.
14. Practice SQL
Act as an SQL trainer. Provide a dataset, give tasks that increase in difficulty, simulate SQL Server results, and review my queries.
15. Interview Prep
Act as an interviewer. Ask common SQL interview questions, progress to advanced topics, evaluate my answers, and give feedback.
16. Exam Prep
Prepare me for a SQL exam with practice questions, progressive difficulty, answer evaluation, and feedback.
- Copy the prompt template
- Replace the placeholder SQL code with your own
- Paste into your AI tool of choice
- Iterate on the response