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

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

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