Prompts: Write, Clean & Document SQL
Prompts #1, #2, #7 and #8: solve a task, improve readability, format code and add documentation.
These four prompts cover the everyday work of writing SQL: getting a first version, making a messy query readable, formatting it, and documenting it.
Definition
AI-assisted query writing means giving the AI your tables and a clear goal, letting it draft the SQL, and then reviewing and testing that SQL yourself before you use it.
The AI does the typing; you stay responsible for correctness.
Prompt #1 — Solve an SQL task
text
In my SQL Server database we have two tables:
- players (id, name, team, country, runs)
- teams (team, team_name, city)
Do the following:
1. Write a query to rank players based on their runs.
2. The result should include player name, team name, runs and their rank.
3. Include comments but avoid commenting on obvious parts.
4. Write three different versions of the query.
5. Evaluate and explain which version is best in terms of readability and performance.Why it works: it gives the tables and columns, a clear task with named output columns, and asks for 3 options plus a verdict, so you learn the alternatives too.
A good answer's version 1 uses RANK():
sql
SELECT
p.name,
t.team_name,
p.runs,
RANK() OVER (ORDER BY p.runs DESC) AS rnk -- 1 = most runs
FROM players AS p
INNER JOIN teams AS t
ON p.team = t.team
ORDER BY rnk;| name | team_name | runs | rnk |
|---|---|---|---|
| Jos Buttler | Rajasthan Royals | 850 | 1 |
| Virat Kohli | Royal Challengers Bengaluru | 750 | 2 |
| David Warner | Delhi Capitals | 700 | 3 |
| Rohit Sharma | Mumbai Indians | 500 | 4 |
| MS Dhoni | Chennai Super Kings | 350 | 5 |
| Jasprit Bumrah | Mumbai Indians | 20 | 6 |
The other versions an AI typically offers are a correlated subquery that counts how many players scored more, and a self-join. The window-function version is usually the most readable and the fastest, because it reads the table once.
Prompt #2 — Improve the readability
text
The following SQL Server query is long and hard to understand.
Do the following:
1. Improve its readability.
2. Remove any redundancy in the query and consolidate it.
3. Include comments but avoid commenting on obvious parts.
4. Explain each improvement to understand the reasoning behind it.
[ SQL Query GOES HERE ]sql
-- before
select p.name,sum(runs) from players p group by p.name
-- after
SELECT
p.name,
SUM(p.runs) AS runs
FROM players AS p
GROUP BY p.name;Why it works: it cleans up messy queries and explains every change, so you learn the style. It's great for old, inherited SQL.
Prompt #7 — Styling and formatting
text
The following SQL Server query is hard to understand.
Do the following:
1. Restyle the code to make it easier to read.
2. Align column aliases.
3. Keep it compact - do not introduce unnecessary new lines.
4. Ensure the formatting follows best practices.
[ SQL Query GOES HERE ]What you get back: consistent uppercase keywords and aliases, readable indentation, and the same result as before. Formatting must never change the logic, so re-run the query to confirm.
Prompt #8 — Documentation and comments
text
The following SQL Server query lacks comments and documentation.
Do the following:
1. Insert a leading comment at the start of the query describing its overall purpose.
2. Add comments only where clarification is necessary, avoiding obvious statements.
3. Create a separate document explaining the business rules implemented by the query.
4. Create another document describing how the query works.
[ SQL Query GOES HERE ]What you get back: a purpose header comment, a business-rules document, and a how-it-works document. That's hours of writing saved, but read them carefully, because the AI may describe business rules that aren't actually in the query.
Common mistakes
- ❌ Pasting the AI's "after" query into production without running it. ✅ Run before and after and compare the results; they must match.
- ❌ Asking for comments on everything. ✅ "Avoid commenting on obvious parts". A comment like
-- select nameadds noise.
Key points to remember
- Prompt #1 (Solve a task): give tables + columns, the exact output columns, and ask for 3 versions plus a verdict.
- Prompt #2 (Readability): ask the AI to remove redundancy and explain each improvement so you learn the style.
- Prompt #7 (Formatting): restyling must never change the result, so re-run the query to confirm.
- Prompt #8 (Documentation): a purpose header, comments only where needed, plus business-rules and how-it-works documents.
- Always compare the 'before' and 'after' results: refactored SQL must return the same rows.
Try it yourself
Prompt #1 answer — version 1 (RANK)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Prompt #1 answer — version 2 (subquery count)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: confirm a refactor returns the same result
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.
