Loading topic
SELECT written inside another SQL statement. Subqueries are the tool you reach for before you know about common table expressions (CTEs), and they are still everywhere in production SQL. This chapter shows the four common forms, builds one query that nests three levels deep, and li7 min readFROM) that you already know, and the errors people hit first. It assumes you can write `SE8 min readWITH clause so that each step builds on the one before it.7 min readWITH clause, you know how JOIN and UNION ALL work, and you have met the idea of a CTE name being visible only inside one statement. The examples run on PostgreSQL. Other dialects differ in small ways, and a later chapter covers those differences.8 min readUNION ALL does. The SQL standard defines recursive CTEs, but each database implements its own subset and adds its own settings. A r7 min readWITH query, chain several CTEs, and read an INSERT, UPDATE, or DELETE statement. It also assumes you know what a transaction is. Everything here is PostgreSQL behavior. Other databases differ, and the dialect section near the end lists the main diff7 min readWITH, chain several CTEs, and read a recursive CTE with an anchor member and a recursive member. It covers the errors that show up most in practice, then the cases where another tool fits better. Examples use PostgreSQL unless a note says otherwi7 min read