Loading topic
SELECT statement nested inside another SQL statement. The outer statement uses the subquery's result as if it were a value, a list of values, or a table. This chapter gives the working model: what a subquery returns, where it can be placed, and the errors you meet first. Later chap6 min readSELECT, WHERE, and ORDER BY, use aggregate functions with GROUP BY, and read joins. It also assumes you know that comparing anything to NULL yields unknown rather than true or false. The chapter covers the simplest subquery shape: a query that produces ex6 min readIN operator that tests a value against them.5 min readIN subqueries, you know how WHERE keeps only rows whose condition is true, and you understand NULL and three-valued logic (true, false, unknown). It covers one bug: NOT IN can return no rows at all when the subquery returns a NULL.5 min readSELECT, WHERE, and GROUP BY queries and use aggregate functions. It also assumes you know how NULL behaves in comparisons, and that you have seen scalar subqueries and IN subqueries from earlier chapters.6 min readNOT IN can fail silently when the subquery returns a NULL. EXISTS and NOT EXISTS avoid that problem. They als6 min readFROM clause. The database runs it first, treats its result as a temporary table, and lets the outer query read from it. This chapter covers the syntax, the rules that trip people up, and the cases where a derived table is the right tool.6 min readINNER JOIN and LEFT JOIN, use GROUP BY with aggregates, and reason about NULL and three-valued logic. It also assumes you have met IN, EXISTS, correlated subqueries, and derived tables in earlier chapters.5 min readIN, correlated, EXISTS, derived table, and CTE subqueries from the earlier chapters. It also assumes you are comfortable with joins and with how NULL behaves in comparisons. The examples use PostgreSQL syntax. MySQL 8 and other engines differ in detai6 min read