Data with Zach

YouTube

What sql should you know in the age of AI

2026-11-20 22:09 UTC
Length: 2:08

SQL fundamentals are still vital for building trustworthy data pipelines that feed AI models. Understanding SELECT, GROUP BY, and ORDER BY lets you read and audit queries more reliably than relying on AI-generated code. Using common table expressions (CTEs) makes complex queries clearer and easier to maintain. Knowing the differences between INSERT, MERGE, and INSERT OVERWRITE protects against accidental data loss when automating data writes.

Insights

1. SQL fundamentals are the backbone of data quality for AI
1.1 SELECT, FROM, ORDER BY, LIMIT, GROUP BY, HAVING are core clauses that let you filter, sort, and aggregate data
1.2 Knowing these lets you verify that AI-generated queries produce correct results
1.3 Mistake: assuming AI always writes optimal SQL; manual review is still necessary

2. Reading SQL is more trustworthy than trusting AI output
2.1 AI can hallucinate or misinterpret schema, leading to errors
2.2 A human can spot syntax or logic mistakes that AI might miss
2.3 Best practice: manually audit AI-generated queries before execution

3. Common table expressions (CTEs) improve query readability
3.1 CTEs use WITH clause to break logic into named subqueries
3.2 They replace ugly nested subqueries and make maintenance easier
3.3 Example: WITH sales AS (SELECT ... ) SELECT * FROM sales WHERE ...

4. INSERT, MERGE, and INSERT OVERWRITE serve different data lake write scenarios
4.1 INSERT INTO adds new rows to an existing table
4.2 MERGE upserts based on a key, updating or inserting as needed
4.3 INSERT OVERWRITE replaces all rows in a table, useful for full refreshes
4.4 Choosing the right command prevents accidental data loss

5. AI agents must be restricted from destructive SQL actions
5.1 Grant minimal privileges to avoid DROP or TRUNCATE commands
5.2 Implement role-based access control and query linting
5.3 Example: deny DROP TABLE to AI service accounts to protect production data

6. AI prefers subqueries; prefer CTEs for maintainability
6.1 Subqueries can be hard to debug and read
6.2 CTEs provide named scopes and can be reused
6.3 When AI outputs nested SELECTs, refactor into CTEs for clarity

Watch on YouTube