Subqueries
A Subquery is a query nested inside another query. They are powerful tools for performing multi-step operations in a single statement.
Definition: Types of Subqueries
Section titled “Definition: Types of Subqueries”Subqueries are categorized by where they are used and what they return.
1. Scalar Subqueries
Section titled “1. Scalar Subqueries”Returns exactly one value (one column, one row). It can be used anywhere a literal value is used (like in the SELECT list or a WHERE clause).
2. Table Subqueries (Derived Tables)
Section titled “2. Table Subqueries (Derived Tables)”Returns an entire table (multiple columns and/or rows). These are used in the FROM clause and must be given an alias.
3. Correlated Subqueries
Section titled “3. Correlated Subqueries”A subquery that references a column from the outer query. Unlike standard subqueries, it cannot be run independently.
Context: EXISTS vs. IN
Section titled “Context: EXISTS vs. IN”This is a classic performance debate.
IN: The subquery is run once, and the results are stored in a list. The outer query then checks its rows against this list.EXISTS: The subquery is run for every row in the outer query. It stops as soon as it finds a single match (Short-circuiting).
Detailed Explanation: Syntax and Examples
Section titled “Detailed Explanation: Syntax and Examples”Scalar Subquery Example
Section titled “Scalar Subquery Example”Find employees who earn more than the company average.
SELECT Name, SalaryFROM EmployeesWHERE Salary > (SELECT AVG(Salary) FROM Employees);Table Subquery Example
Section titled “Table Subquery Example”Find the maximum of a set of averages.
SELECT MAX(AvgSalary)FROM ( SELECT Dept, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Dept) AS DepartmentStats;Correlated Subquery Example
Section titled “Correlated Subquery Example”Find products that are priced higher than the average for their specific category.
SELECT p1.Name, p1.PriceFROM Products p1WHERE p1.Price > ( SELECT AVG(p2.Price) FROM Products p2 WHERE p2.Category = p1.Category);Example: The “Existence” Pattern
Section titled “Example: The “Existence” Pattern”We want to find Customers who have placed at least one order.
SELECT NameFROM Customers cWHERE EXISTS ( SELECT 1 FROM Orders o WHERE o.CustomerID = c.ID);Under the Hood: The Optimizer’s View
Section titled “Under the Hood: The Optimizer’s View”How does the database execute a subquery?
1. Flattening
Section titled “1. Flattening”Modern query optimizers are very smart. Often, they will “flatten” a subquery into a standard JOIN if it’s more efficient.
2. The Loop Trap
Section titled “2. The Loop Trap”Correlated subqueries are often slow because, in the worst-case scenario, the database performs a Nested Loop. If the outer table has 100,000 rows, the subquery might be executed 100,000 times.
3. Materialization
Section titled “3. Materialization”For non-correlated subqueries in the FROM clause, the database might “materialize” the subquery—meaning it runs the subquery, saves the result to a temporary table in memory, and then joins that temp table to the rest of your query.