The AI + SQL Workflow & Golden Rules
Ask → Review → Test → Refine, the 6 golden rules, data safety, and all 16 prompts at a glance.
AI can sound confident and still be wrong. It can invent a column, use the wrong dialect, or quietly drop rows with the wrong JOIN. You are responsible for the final query. This topic is the habit that keeps AI useful and safe.
Definition
The AI + SQL workflow is a four-step loop: Ask a clear prompt → Review the answer → Test it on data → Refine with feedback. You repeat it until the query is proven correct.
The workflow: never copy-paste blindly
| Step | What you do | Question to ask yourself |
|---|---|---|
| 1. Ask | Write a clear prompt with context, task and specifications | Did I give the engine, tables and expected output? |
| 2. Review | Read the answer line by line | Do the tables and columns actually exist? Is the JOIN type right? |
| 3. Test | Run it on a small sample and compare with a result you know | Does the row count make sense? Are NULLs handled? |
| 4. Refine | Found a problem? Paste the error or the wrong result back and ask again (prompt #5) | What exactly is wrong, and can I show the AI? |
6 golden rules of AI + SQL
| Rule | Meaning | Example |
|---|---|---|
| 1. Be specific | Database engine, tables, columns, expected output | "SQL Server + players (id, name, team, country, runs)" |
| 2. Give structure | Use "Do the following:" and a numbered list | 1. 2. 3. |
| 3. Ask why | Request an explanation so you learn, not just copy | "Explain each step" |
| 4. Iterate | Refine the prompt with errors and feedback | See prompt #5 |
| 5. Always test | Verify on sample data and the execution plan | Never trust blindly |
| 6. Stay safe | Keep sensitive data out of your prompts | Mask real data |
How to test an AI query quickly
- Know one answer in advance. For example, Jos Buttler has the most runs (850). If the AI's "top scorer" query doesn't say Buttler, it's wrong.
- Check the row count. 6 players exist. A JOIN that returns 4 rows silently dropped 2.
- Look for NULLs. Does the query handle Warner's NULL team in
player_profiles, or Dhoni's NULL innings? - Check the dialect.
LIMITin a "SQL Server" answer means the AI ignored your context.
Data safety
Warning: Never paste passwords, connection strings, customers' personal data or confidential company data into a public AI tool. Share the structure (table and column names) and fake sample rows instead. That's enough for the AI to help.
Bonus: the 16 prompts at a glance
| # | Prompt | # | Prompt |
|---|---|---|---|
| 1 | Solve an SQL Task | 9 | Improve Database DDL |
| 2 | Improve Readability | 10 | Generate Test Dataset |
| 3 | Optimize Performance | 11 | Create an SQL Course |
| 4 | Optimize Execution Plan | 12 | Understand a Concept |
| 5 | Debugging | 13 | Compare Concepts |
| 6 | Explain the Result | 14 | Practice SQL |
| 7 | Styling & Formatting | 15 | SQL Interview Prep |
| 8 | Documentation & Comments | 16 | SQL Exam Prep |
Next up: real SQL projects. Put everything from this course together.
Key points to remember
- The workflow is Ask → Review → Test → Refine, repeated until the query is proven correct.
- AI can be confident and wrong; you are responsible for the final query.
- Test with a result you already know, check row counts, look for NULL handling, and check the dialect.
- The 6 golden rules: be specific, give structure, ask why, iterate, always test, stay safe.
- Never paste passwords, personal or confidential data into a public AI tool; share structure and fake rows instead.
Try it yourself
Test an AI answer against a known result
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Row-count check: did the JOIN drop anyone?
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: review an AI query before trusting it
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.
