Writing Good SQL Prompts
The five parts of a strong prompt, weak vs strong prompts, and where chat and code assistants help with SQL.
AI assistants like ChatGPT, Claude, Gemini or GitHub Copilot can write, fix and explain SQL in seconds — but only as well as you ask. A vague question gets a generic guess. A precise question gets a query that runs on your tables. This chapter gives you 16 copy-paste prompts that work, all tried on our IPL data.
Definition
A prompt is the instruction you give an AI assistant. A good SQL prompt tells the AI what to do (task), what it is working with (context: database engine, tables, columns), and what the answer must look like (specifications).
The AI cannot see your database. Everything it knows about your tables comes from the prompt. Every missing detail becomes a guess, and guesses produce wrong column names, the wrong SQL dialect, or a query that answers a different question.
The five prompt components
| Component | Priority | Question it answers | IPL example |
|---|---|---|---|
| Task | Mandatory | What should the AI do? | Write a query that ranks players by runs. |
| Context | Important | What background does it need? | SQL Server. Tables: players (id, name, team, country, runs) and teams (team, team_name, city). |
| Specifications | Important | How should the answer look? | Add comments. Return 3 versions. Explain which is fastest. |
| Role | Nice to have | Who should the AI act as? | Act as a senior SQL developer. |
| Tone | Nice to have | How should it speak? | Explain it simply, like I am a beginner. |
Tip: Always name the database engine.
TOP 3works in SQL Server, but MySQL and PostgreSQL needLIMIT 3.LEN()is SQL Server,LENGTH()is MySQL/PostgreSQL. If you don't say which one you use, the AI picks one for you.
Weak prompt vs strong prompt
Weak:
text
Write SQL for players- ❌ Which database? ❌ Which tables and columns? ❌ What should the query return?
- Result: a generic guess that probably won't run on your data.
Strong:
text
Act as a senior SQL developer. ← Role
In my SQL Server database I have:
players (id, name, team, country, runs). ← Context
Write a query that ranks players by runs. ← Task
Return id, name, team, runs, rank. Add comments. ← Specifications
Explain your solution simply. ← ToneThe more detail you put in the prompt, the fewer wrong guesses the AI makes.
Where AI can help you with SQL
There are two kinds of assistants, and they are good at different jobs:
| Chat assistant (ChatGPT, Claude, Gemini) | Code assistant in your editor (Copilot, Cursor) |
|---|---|
| Brainstorming and ideas | Coding assistance while you type |
| Project planning | Debugging |
| Learning, knowledge and research | Refactoring |
| Generating documentation | Inline comments |
| Discussing architecture | Quick fixes |
| Exploring best practices | Styling and formatting code |
| Complex problem-solving |
The "Do the following:" structure
Every prompt in this chapter uses the same skeleton. It works because the AI answers each numbered point in order, so nothing gets skipped:
text
<one or two lines of context: engine, tables, the situation>
Do the following:
1. <first thing>
2. <second thing>
3. <third thing>
[ SQL Query GOES HERE ]Common mistakes
- ❌ Leaving out the table structure, so the AI invents columns like
player_nameortotal_runs. ✅ Paste the column list or theCREATE TABLE. - ❌ Asking for "the best query" with no definition of best. ✅ Say "best for readability" or "fastest on a large table".
- ❌ Accepting the first answer. ✅ Iterate: paste the error or a wrong result back and ask again.
Key points to remember
- A prompt is the instruction you give the AI; it can only use what you tell it about your database.
- The five components: Task (mandatory), Context and Specifications (important), Role and Tone (nice to have).
- Always state the database engine (SQL Server, MySQL, PostgreSQL), because syntax like TOP vs LIMIT differs.
- Give the exact tables and columns, otherwise the AI guesses column names.
- Chat assistants are good for learning, planning and design; code assistants are good for writing, fixing and formatting code in the editor.
- Structure prompts with 'Do the following:' and a numbered list so every requirement gets answered.
Try it yourself
What the strong prompt should return — verify it
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: check the AI's columns exist
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.
