Bird
0
0

What will be the output of this query?

medium📝 query result Q5 of 15
PostgreSQL - Window Functions in PostgreSQL
What will be the output of this query?
SELECT name, score, DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank FROM players;

Given scores: 50, 40, 40, 30.
ADense ranks: 1, 2, 3, 4
BDense ranks: 1, 2, 2, 3
CDense ranks: 1, 1, 2, 3
DDense ranks: 1, 1, 1, 2
Step-by-Step Solution
Solution:
  1. Step 1: Understand DENSE_RANK() behavior

    DENSE_RANK() assigns same rank to ties without gaps.
  2. Step 2: Assign dense ranks to scores

    Score 50 gets 1, scores 40 get 2, score 30 gets 3.
  3. Final Answer:

    Dense ranks: 1, 2, 2, 3 -> Option B
  4. Quick Check:

    DENSE_RANK() no gaps in ranks = Dense ranks: 1, 2, 2, 3 [OK]
Quick Trick: DENSE_RANK() assigns consecutive ranks even with ties [OK]
Common Mistakes:
  • Expecting gaps in dense ranks
  • Confusing DENSE_RANK() with RANK()
  • Misreading order direction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes