SQL for GenAI¶
Beginner → Advanced · 9 topics
Most company data lives in SQL databases — orders, customers, tickets, payments. GenAI engineers meet SQL in three places:
- Feeding LLM apps: fetching the customer's orders for a support bot, filtering documents for RAG.
- Measuring LLM apps: LLM call logs, costs, latencies and eval results are tables. SQL answers "which feature costs the most?" or "did prompt v3 beat v2?" in seconds.
- Text-to-SQL: letting users ask questions in plain English and having an LLM write the query — one of the most requested enterprise GenAI features, and one of the riskiest.
Topics 1–6 use SQLite, which comes with Python — nothing to install, and every query runs. The SQL is standard and works in PostgreSQL, MySQL and SQL Server with small changes (noted where they matter). Topics 7–9 then cover each of those three databases: the dialect, the traps and the features interviews ask about.
| # | Topic | Sub-topics |
|---|---|---|
| 1 | SQL basics with Python | Tables · SELECT · WHERE · ORDER BY · LIMIT · sqlite3 · parameterised queries |
| 2 | Joins & aggregation | GROUP BY · HAVING · INNER / LEFT JOIN · subqueries · NULL traps |
| 3 | Advanced SQL | CTEs · window functions · CASE · dates · JSON columns · indexes · pgvector |
| 4 | Querying LLM logs & evals | Cost per feature · p95 latency · error rates · comparing prompt versions · regressions |
| 5 | Text-to-SQL | Schema in the prompt · few-shot · validating SQL · read-only safety · evaluating accuracy |
| 6 | Text-to-SQL agents | Tools for exploring a database · the agent loop · self-correction · production guardrails |
| 7 | MySQL | utf8mb4 & collations · ON DUPLICATE KEY UPDATE · JSON + generated-column indexes · FULLTEXT · EXPLAIN · InnoDB locks · SKIP LOCKED |
| 8 | PostgreSQL | timestamptz · RETURNING · ON CONFLICT · DISTINCT ON · jsonb + GIN · full-text search · pgvector · row-level security · MVCC |
| 9 | SQL Server | T-SQL · TOP / OFFSET · OUTPUT · MERGE · CROSS APPLY · OPENJSON · temporal tables · execution plans · RCSI |
Which database?¶
| MySQL | PostgreSQL | SQL Server | |
|---|---|---|---|
| Typical home | web apps, e-commerce, WordPress | new apps, startups, GenAI stacks | enterprises, banks, .NET, ERPs |
| Licence | open source (GPL) + Oracle editions | open source (PostgreSQL licence) | commercial; free Developer and Express editions |
| Managed cloud | RDS, Aurora, Cloud SQL, Azure | RDS, Aurora, Cloud SQL, AlloyDB, Azure, Supabase, Neon | Azure SQL, RDS |
| Default isolation | REPEATABLE READ | READ COMMITTED | READ COMMITTED (locking; RCSI optional) |
| First N rows | LIMIT n |
LIMIT n |
TOP (n) / OFFSET … FETCH |
| Upsert | ON DUPLICATE KEY UPDATE |
ON CONFLICT … DO UPDATE · MERGE |
MERGE · UPDATE + INSERT |
| Return written rows | — | RETURNING |
OUTPUT |
| JSON | JSON + generated-column indexes |
jsonb + GIN indexes |
NVARCHAR + JSON_VALUE (native json in 2025) |
| Full-text search | FULLTEXT + MATCH … AGAINST |
tsvector + GIN |
full-text catalog + CONTAINS |
| Vector search | VECTOR type (9.0+); search in HeatWave |
pgvector (HNSW, IVFFlat) | vector + VECTOR_DISTANCE (2025) |
| Python driver | PyMySQL (%s) |
psycopg 3 (%s) |
pyodbc (?) |
Practise and go further
Free: the SQL Interview Cheat Sheet, a PDF with 25 interview
problems and every query's real output, for quick revision.
In depth: The Complete SQL Guide, a developer's
reference that teaches every topic in this series step by step, then adds 15 production patterns (safe
parameters, keyset pagination, upserts, job queues, migrations), metric formulas and 30 solved interview problems.
Where this fits
Read after Python Basics. Pandas comes next — and
pd.read_sql() turns any query from this series into a DataFrame.
Next: Pandas — tables of data in Python.