Advanced SQL patterns replace imperative loops with set-based operations. Window functions (ROW_NUMBER, RANK, LAG, LEAD) compute across rows without collapsing them like GROUP BY. CTEs (WITH clauses) decompose complex queries and enable recursive tree/hierarchy queries. Lateral joins let a subquery reference outer row columns. EXISTS is usually faster than IN for correlated subqueries because it short-circuits on first match.
Recursive CTE for hierarchical org chart traversal.
LAG(salary) OVER (PARTITION BY dept ORDER BY hire_date) gets previous employee salary without a self-join. Same performance, far more readable.
SELECT * FROM orders WHERE EXISTS (SELECT 1 FROM returns WHERE returns.order_id = orders.id) short-circuits. IN builds the full set first.
OFFSET 10000 scans and discards 10,000 rows. Keyset: WHERE id > 10000 ORDER BY id LIMIT 20 uses the index directly — O(log n) vs O(n).
Sign in to share your feedback and join the discussion.