TASK
Use SQL window functions
Window functions (OVER) perform calculations across rows without grouping. Learn ROW_NUMBER, RANK, LAG, LEAD, and aggregate windows.
- 1
Window functions operate on a "window" of rows related to the current row, without collapsing them like GROUP BY. Syntax: function() OVER (PARTITION BY ... ORDER BY ...). Common functions: ROW_NUMBER (sequential), RANK (with gaps), DENSE_RANK (no gaps), LAG/LEAD (previous/next row), SUM/AVG (running totals).
- 2 SQL Formatter →
Format queries with window functions to verify the PARTITION BY and ORDER BY clauses. The window specification determines which rows are included in the calculation. Format makes the window frame explicit and helps debug incorrect results.
Frequently asked questions
- ROW_NUMBER vs RANK vs DENSE_RANK?
- ROW_NUMBER: unique sequential number (1, 2, 3, 4). RANK: same rank for ties, gaps after (1, 2, 2, 4). DENSE_RANK: same rank for ties, no gaps (1, 2, 2, 3). Use ROW_NUMBER for unique row identification, RANK for competitive ranking with gaps, DENSE_RANK for sequential ranking without gaps.
- Window functions vs GROUP BY?
- GROUP BY collapses rows into groups (one row per group). Window functions keep all rows and add a calculated column. Use GROUP BY for aggregation (total per category); use window functions for per-row calculations with context (running total, rank within category, previous value).
Workflows that solve this
Learn more
Want to understand why?
How to Analyze a Query Plan
An advanced-level guide covering query-plan, database, optimization. Includes step-by-step instructions, common errors, troubleshooting tips, and security considerations for query-plan. TUTORIAL
How to Choose a Consistency Level
An advanced-level guide covering consistency, database, distributed. Includes step-by-step instructions, common errors, troubleshooting tips, and security considerations for consistency. TUTORIAL
ACID Property
ACID is the four guarantees of transactions: atomicity, consistency, isolation, and durability, ensuring reliable transaction processing. GLOSSARY
Anti Join
An anti join returns rows from the outer table that have no match in the inner table, used to optimize NOT EXISTS / NOT IN subqueries. GLOSSARY
Related tools
SQL Formatter
Beautify or minify SQL queries online. Uppercase keywords, indent clauses, or compress to a single line. Free, private, and 100% client-side. TOOL
Table Editor & Converter
Edit tabular data and export to CSV/JSON/HTML/Markdown. Free, private, 100% client-side. TOOL
JSON Validator
Validate JSON and pinpoint syntax errors by line and column. Free, private, and 100% client-side. TOOL