Bird
Raised Fist0
PostgreSQLquery~10 mins

ROW_NUMBER, RANK, DENSE_RANK in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ROW_NUMBER, RANK, DENSE_RANK
Start with ordered data
↓
Apply ROW_NUMBER
↓
Apply RANK
↓
Apply DENSE_RANK
↓
Compare results
↓
End
We start with data sorted by a column, then apply ROW_NUMBER, RANK, and DENSE_RANK functions to assign row positions with different rules for ties.
Execution Sample
PostgreSQL
SELECT name, score,
  ROW_NUMBER() OVER (ORDER BY score DESC) AS row_num,
  RANK() OVER (ORDER BY score DESC) AS rank,
  DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank
FROM players;
This query assigns row numbers, ranks, and dense ranks to players ordered by their score descending.
Execution Table
StepRowNameScoreROW_NUMBERRANKDENSE_RANKExplanation
11Alice100111Highest score, first row
22Bob90222Second highest score, no tie
33Charlie90322Tie with Bob, same rank 2, row_number increments
44David80443Next score after tie, rank skips to 4, dense_rank increments by 1
55Eve70554Lowest score, ranks increment normally
💡 All rows processed; ranking functions assigned based on score order with tie rules.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5
ROW_NUMBER012345
RANK012245
DENSE_RANK012234
Key Moments - 2 Insights
Why does RANK assign the same number to Bob and Charlie but ROW_NUMBER does not?
RANK gives the same rank to tied scores (Bob and Charlie both 90 get rank 2), but ROW_NUMBER always gives a unique sequential number regardless of ties (Bob is 2, Charlie is 3). See execution_table rows 2 and 3.
Why does RANK skip a number after a tie but DENSE_RANK does not?
RANK skips ranks after ties to reflect gaps (after rank 2 tie, next rank is 4). DENSE_RANK assigns consecutive ranks without gaps (after dense rank 2 tie, next is 3). See execution_table rows 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the ROW_NUMBER for Charlie at step 3?
A2
B3
C1
D4
💡 Hint
Check the ROW_NUMBER column in execution_table row 3.
At which step does the RANK skip a number after a tie?
AStep 4 to 5
BStep 2 to 3
CStep 3 to 4
DStep 1 to 2
💡 Hint
Look at RANK values in execution_table rows 3 and 4.
If David's score changed to 90, what would happen to DENSE_RANK at step 4?
AIt would become 2
BIt would remain 3
CIt would become 4
DIt would become 1
💡 Hint
DENSE_RANK assigns same rank to ties; see how Bob and Charlie share dense_rank 2.
Concept Snapshot
ROW_NUMBER assigns unique sequential numbers to rows.
RANK assigns ranks with gaps after ties.
DENSE_RANK assigns ranks without gaps for ties.
All require ORDER BY inside OVER() clause.
Used to rank rows based on ordering criteria.
Full Transcript
This visual execution shows how PostgreSQL ranking functions ROW_NUMBER, RANK, and DENSE_RANK work on a list of players ordered by score descending. ROW_NUMBER gives each row a unique number regardless of ties. RANK assigns the same rank to tied scores but skips numbers after ties. DENSE_RANK assigns the same rank to ties but does not skip numbers. The execution table traces each row's assigned values step-by-step. Variable tracking shows how these values change as rows are processed. Key moments clarify common confusions about ties and numbering. The quiz tests understanding of these differences using the execution visuals.

Practice

(1/5)
1. What does the ROW_NUMBER() function do in PostgreSQL?
easy
A. Counts the total number of rows in the table.
B. Assigns the same rank to rows with identical values, skipping ranks after ties.
C. Assigns ranks to rows without skipping numbers after ties.
D. Assigns a unique sequential number to each row in the result set.

Solution

  1. Step 1: Understand ROW_NUMBER() purpose

    ROW_NUMBER() gives each row a unique sequential number, starting at 1.
  2. Step 2: Compare with RANK() and DENSE_RANK()

    Unlike RANK() or DENSE_RANK(), ROW_NUMBER() never assigns the same number to multiple rows.
  3. Final Answer:

    Assigns a unique sequential number to each row in the result set. -> Option D
  4. Quick Check:

    ROW_NUMBER() = unique row numbers [OK]
Hint: ROW_NUMBER always gives unique numbers, no ties allowed [OK]
Common Mistakes:
  • Confusing ROW_NUMBER with RANK or DENSE_RANK
  • Thinking ROW_NUMBER skips numbers after ties
  • Assuming ROW_NUMBER counts total rows
2. Which of the following is the correct syntax to assign a rank using RANK() over a column named score ordered descending?
easy
A. SELECT RANK(score) OVER (ORDER BY DESC) FROM table_name;
B. SELECT RANK() ORDER BY score DESC FROM table_name;
C. SELECT RANK() OVER (ORDER BY score DESC) FROM table_name;
D. SELECT RANK() OVER (PARTITION BY score DESC) FROM table_name;

Solution

  1. Step 1: Recall correct RANK() syntax

    RANK() is used with OVER() clause specifying ORDER BY column and direction.
  2. Step 2: Check each option

    SELECT RANK() OVER (ORDER BY score DESC) FROM table_name; correctly uses RANK() OVER (ORDER BY score DESC). Others misuse syntax or clauses.
  3. Final Answer:

    SELECT RANK() OVER (ORDER BY score DESC) FROM table_name; -> Option C
  4. Quick Check:

    RANK() OVER (ORDER BY column DESC) syntax [OK]
Hint: RANK() needs OVER with ORDER BY column [OK]
Common Mistakes:
  • Putting column inside RANK() function
  • Using PARTITION BY incorrectly
  • Omitting OVER() clause
3. Given the table players with columns name and score, what will be the output of this query?
SELECT name, score, RANK() OVER (ORDER BY score DESC) AS rank FROM players;

Data:
name | score -----|------ Alice | 100 Bob | 90 Carol | 90 Dave | 80
medium
A. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 1}, {"name": "Carol", "score": 90, "rank": 1}, {"name": "Dave", "score": 80, "rank": 2}]
B. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 2}, {"name": "Dave", "score": 80, "rank": 4}]
C. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 2}, {"name": "Dave", "score": 80, "rank": 3}]
D. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 3}, {"name": "Dave", "score": 80, "rank": 4}]

Solution

  1. Step 1: Understand RANK() behavior with ties

    RANK() assigns the same rank to tied scores and skips ranks after ties.
  2. Step 2: Apply ranks to scores

    Alice has highest score 100 -> rank 1. Bob and Carol tie at 90 -> both rank 2. Next rank after tie skips to 4 for Dave.
  3. Final Answer:

    [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 2}, {"name": "Dave", "score": 80, "rank": 4}] -> Option B
  4. Quick Check:

    RANK() skips ranks after ties [OK]
Hint: RANK() skips numbers after ties, DENSE_RANK() does not [OK]
Common Mistakes:
  • Assuming ranks after ties are consecutive
  • Confusing RANK() with DENSE_RANK()
  • Misordering rows by score
4. Identify the error in this query:
SELECT name, DENSE_RANK(score) OVER (ORDER BY score DESC) FROM players;
medium
A. DENSE_RANK() should not have a column inside the parentheses.
B. DENSE_RANK() cannot be used without PARTITION BY clause.
C. ORDER BY clause is missing in the OVER() clause.
D. The query is correct and will run without errors.

Solution

  1. Step 1: Check DENSE_RANK() syntax

    DENSE_RANK() does not take any arguments inside parentheses; it is called as DENSE_RANK() OVER (...).
  2. Step 2: Identify the error in the query

    The query incorrectly uses DENSE_RANK(score), which causes a syntax error.
  3. Final Answer:

    DENSE_RANK() should not have a column inside the parentheses. -> Option A
  4. Quick Check:

    DENSE_RANK() has empty parentheses [OK]
Hint: Window functions have empty parentheses, column goes in ORDER BY [OK]
Common Mistakes:
  • Putting column inside DENSE_RANK()
  • Thinking PARTITION BY is mandatory
  • Omitting OVER() clause
5. You want to assign ranks to employees based on their sales, but you want no gaps in ranking numbers even if some employees have the same sales. Which function should you use and why?

Options:
A) ROW_NUMBER() because it gives unique numbers.
B) RANK() because it skips ranks after ties.
C) DENSE_RANK() because it assigns ranks without gaps after ties.
D) COUNT() because it counts rows.
hard
A. DENSE_RANK() because it assigns ranks without gaps after ties.
B. RANK() because it skips ranks after ties.
C. ROW_NUMBER() because it gives unique numbers.
D. COUNT() because it counts rows.

Solution

  1. Step 1: Understand ranking requirements

    The requirement is to assign ranks with no gaps even if employees tie in sales.
  2. Step 2: Match functions to behavior

    ROW_NUMBER() gives unique numbers, ignoring ties. RANK() skips ranks after ties causing gaps. DENSE_RANK() assigns ranks without gaps after ties.
  3. Step 3: Choose correct function

    DENSE_RANK() fits the requirement perfectly.
  4. Final Answer:

    DENSE_RANK() because it assigns ranks without gaps after ties. -> Option A
  5. Quick Check:

    DENSE_RANK() = no gaps in ranks [OK]
Hint: Use DENSE_RANK() to avoid gaps in ranking numbers [OK]
Common Mistakes:
  • Choosing RANK() and expecting no gaps
  • Using ROW_NUMBER() which ignores ties
  • Confusing COUNT() with ranking functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard