10
Chapter 10
Subqueries & EXISTS
Queries inside queries. Scalar, correlated, EXISTS, IN, ANY, ALL — and when a CTE would read better.
In this chapter
21
queries, easy → advanced
- 001Subquery in WHERE: employees earning above averageRead
- 002Subquery in FROM (derived table)Read
- 003EXISTS: find customers who have placed ordersRead
- 004NOT EXISTS: customers who never orderedRead
- 005IN with subquery: employees in engineering departmentsRead
- 006Correlated subquery: find department average for each employeeRead
- 007Subquery in SELECT: each order with its item countRead
- 008ANY / ALL: employees earning more than ANY managerRead
- 009Subquery with aggregation to find highest-earning deptRead
- 010Multi-level nested subqueryRead
- 011Subquery to find gaps in sequential IDsRead
- 012Recursive subquery: find all ancestors of a categoryRead
- 013Find products priced above averageRead
- 014Find customers who spent more than average customerRead
- 015Find departments with no active projectsRead
- 016Products never ordered this yearRead
- 017Subquery with LIMIT: second highest salary per departmentRead
- 018Find the employee who has the most project hours in each deptRead
- 019Find customers with orders in consecutive monthsRead
- 020Classic: employees earning more than their managerRead
- 021Classic: median salary without PERCENTILE_CONTRead