What is the difference between RANK and DENSE_RANK in SQLite?
Quick answer
In SQLite, RANK() gives tied rows the same rank and leaves a gap before the next rank. DENSE_RANK() also gives tied rows the same rank, but continues with the next consecutive number. Use PARTITION BY to restart either ranking within each group. The window’s ORDER BY determines which values tie; a separate final ORDER BY can make displayed rows easier to read. [S1]
The question, the details, the next step.
A practical checklist
Choose
RANK()for gaps after ties orDENSE_RANK()for consecutive score-level numbers.Put the group column in
PARTITION BYif each group should restart at rank 1.Use only the intended tie-defining columns in the window’s
ORDER BY.Use the query-level
ORDER BYfor readable row display without changing ties.Check the query with a sample that includes tied values and more than one group.
A few more useful answers
Does `PARTITION BY` restart both functions?
Yes. Each partition is ranked independently, so the rank numbering starts over for each group. [S1]
Can I use both functions in one SQLite query?
Yes. As in the example, select both window-function expressions, giving each a different alias.
Does the final `ORDER BY` determine whether rows tie?
No. The window’s ORDER BY defines peers for ranking; the final query ORDER BY determines the displayed row order. [S1]
Sources
References used for this guide.
- [S1] SQLite Window Functions: rank() and dense_rank() sqlite.org · Checked
