beginnerWindow Functions — ROW_NUMBER, RANK, LEAD, LAG

What is the difference between ROW_NUMBER(), RANK(), and DENSE_RANK()?

All three assign numbers to rows within a window, but handle ties differently. ROW_NUMBER(): always unique — assigns sequential 1,2,3... even for tied values. Arbitrary which tie gets lower number (depends on physical order or secondary sort). RANK(): assigns same rank to ties, then SKIPS the next number(s): 1,1,3,4. DENSE_RANK(): assigns same rank to ties but does NOT skip: 1,1,2,3. Example with

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

What is the difference between ROW_NUMBER(), RANK(), and DENSE_RANK()?

Next Step

Continue to How do you find the top-N rows per group using window functions?← Back to all SQL questions