RANK vs DENSE_RANK vs ROW_NUMBER in SQL: Complete Comparison
By Sharon Ben-Moshe · October 1, 2026 · 2 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Compare RANK, DENSE_RANK, and ROW_NUMBER in SQL. See visual examples of how ties and numbering gaps behave across PostgreSQL, MySQL, and SQL Server.
Quick answer
The difference between ROW_NUMBER, RANK, and DENSE_RANK lies in how they handle ties. ROW_NUMBER always assigns unique sequential integers (1, 2, 3, 4) regardless of ties. RANK assigns identical numbers to ties and skips subsequent numbers (1, 2, 2, 4). DENSE_RANK assigns identical numbers to ties without skipping subsequent numbers (1, 2, 2, 3).
FAQs
When should I use ROW_NUMBER instead of RANK?
Use ROW_NUMBER when you need an absolute unique identifier per row for pagination, deduplication (picking arbitrary top 1), or when ties must still be sorted into distinct slots.
When should I use DENSE_RANK?
Use DENSE_RANK for competition-style rankings where you want to know the top N distinct tiers (e.g., top 3 highest salary bands) without skipping positions after ties.
What happens if ORDER BY is omitted in these window functions?
Most SQL engines require an ORDER BY clause inside the OVER() clause for ROW_NUMBER, RANK, and DENSE_RANK. Without ORDER BY, ranking is undefined and throws a syntax error.
Are these functions supported in SQLite and MySQL?
Yes. SQLite added window functions in version 3.25.0, and MySQL added them in version 8.0. PostgreSQL, SQL Server, and BigQuery have supported them for years.
Keep reading
The Comprehensive Guide to SQL Data Types: Storage, Precision, and Cross-Dialect Portability
By Sharon Ben-Moshe · Oct 13, 2026
The complete guide to SQL data types across PostgreSQL, MySQL, SQL Server, and SQLite. Numeric precision, character sets, timestamps, and JSON.
Read postThe Definitive Guide to SQL Window Functions: Syntax, Framing, and Real-World Recipes
By Sharon Ben-Moshe · Oct 13, 2026
The comprehensive guide to SQL window functions. Master OVER, PARTITION BY, ORDER BY, window frames (ROWS vs RANGE), and ranking recipes.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.