The three ranking functions
Three window functions for assigning numbers within partitions. They differ in how they handle ties:
- ROW_NUMBER() — unique sequential number, no ties (1, 2, 3, 4).
- RANK() — ties share the rank, then skips (1, 2, 2, 4).
- DENSE_RANK() — ties share the rank, no skipping (1, 2, 2, 3).
Top-N per group
The classic application: "show me the 3 most recent orders per customer." Subquery with ROW_NUMBER, then filter WHERE rn <= 3. This single pattern shows up in nearly every dashboard you'll ever build.
CTEs for readable layered queries
A CTE (Common Table Expression, the WITH clause) is a named subquery defined at the top of your query. It turns deeply nested queries into named, readable steps. Multiple CTEs can chain. Recursive CTEs traverse trees.