Skip to main content
Back to SQL Coding

SQL Coding · Doing It With AI

Directing AI to Write SQL

The one idea: You don't need to write SQL if you can tell an AI exactly what to ask for and then check its answer. Give it the tables, what one row means, and a rough size for the result — then verify the query against real data instead of trusting how confident it sounds.
⚠️ The trap: The AI's query ran, returned clean numbers, and its explanation sounds right — so I can trust the result. That feels right and it's wrong. The database only catches syntax errors; every expensive bug runs cleanly, and re-asking the AI "is this correct?" just earns a confident yes — verify against real data, not against more prose.

You can read a query, spot the classic bugs, and follow nested logic — which means you no longer need someone else to write your SQL. You need an AI to write it and you to direct and verify it. That's a genuine capability, not a workaround: the person who knows exactly what question to ask and can audit the answer is doing the analytical work. The AI is doing the typing. This lesson is the operating manual for that division of labor.

What context to give the AI

An AI writing SQL without context is guessing at your schema — inventing plausible table names, assuming columns exist, assuming one row means what it usually means at other companies. Every piece of context you provide converts a guess into a constraint. Provide four things:

1. The table schemas. Actual table and column names with types. Paste the DDL (the CREATE TABLE definitions) if you have it,…

Keep reading: Directing AI to Write SQL

Unlock the full SQL Coding track — every lesson, the AI tutor, and full mock assessments.

  • Full lesson content
  • AI tutor for this section
  • Practice questions