SQL Window Functions and CTEs
Use CTEs, recursive queries, ranking, running totals, partitions, and lag/lead patterns for analytical SQL.
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Common Table Expressions
WITH RECURSIVE org_tree AS ( SELECT employee_id, manager_id, name, 1 AS depth FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.employee_id, e.manager_id, e.name, ot.depth + 1 FROM employees e JOIN org_tree ot ON ot.employee_id = e.manager_id ) SELECT * FROM org_tree ORDER BY depth, employee_id;
# Traverse hierarchical or graph-like data.
## Window Function Basics
## Lag, Lead, and Value Navigation
## Buckets and Percentiles
More in SQL
SQL Indexing and Performance
Query tuning patterns with EXPLAIN, indexing, keyset pagination, deduplication, and optimization-oriented SQL recipes.
SQL DDL and DML
Create and change schema objects, then insert, update, delete, merge, and manage transactional data changes.
SQL Functions and Aggregation
Reference for string, date, numeric, conditional, null-handling, and aggregate SQL functions.
SQL Joins and Subqueries
Advanced relational query patterns for joins, subqueries, EXISTS, IN, set operations, and correlated queries.
SQL Cheat Sheet
Core SQL syntax and patterns for querying, filtering, sorting, joining, grouping, and modifying data.