Bird
0
0

What is the main difference between ROW_NUMBER() and RANK() functions in PostgreSQL?

easy📝 Conceptual Q1 of 15
PostgreSQL - Window Functions in PostgreSQL
What is the main difference between ROW_NUMBER() and RANK() functions in PostgreSQL?
A<code>ROW_NUMBER()</code> and <code>RANK()</code> always produce identical results
B<code>ROW_NUMBER()</code> assigns the same rank to ties, <code>RANK()</code> assigns unique sequential numbers
C<code>ROW_NUMBER()</code> assigns unique sequential numbers, <code>RANK()</code> assigns the same rank to ties
D<code>ROW_NUMBER()</code> is used only for grouping, <code>RANK()</code> is used only for ordering
Step-by-Step Solution
Solution:
  1. Step 1: Understand ROW_NUMBER() behavior

    ROW_NUMBER() gives a unique number to each row, even if values are tied.
  2. Step 2: Understand RANK() behavior

    RANK() assigns the same rank to rows with equal values, causing gaps in numbering.
  3. Final Answer:

    ROW_NUMBER() assigns unique sequential numbers, RANK() assigns the same rank to ties -> Option C
  4. Quick Check:

    Difference between ROW_NUMBER and RANK = ROW_NUMBER() assigns unique sequential numbers, RANK() assigns the same rank to ties [OK]
Quick Trick: ROW_NUMBER never repeats numbers; RANK repeats for ties [OK]
Common Mistakes:
  • Confusing ROW_NUMBER with RANK behavior on ties
  • Assuming both functions produce identical results
  • Thinking RANK never skips numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes