0
0
PostgreSQLquery~5 mins

ROW_NUMBER, RANK, DENSE_RANK in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ROW_NUMBER() function do in SQL?
ROW_NUMBER() assigns a unique sequential number to each row in the result set, starting at 1, without skipping any numbers, even if there are ties.
Click to reveal answer
intermediate
How does RANK() differ from ROW_NUMBER() when there are ties?
RANK() assigns the same rank to tied rows but skips the next ranks accordingly. For example, if two rows tie for rank 1, the next rank will be 3, not 2.
Click to reveal answer
intermediate
Explain DENSE_RANK() in simple terms.
DENSE_RANK() assigns the same rank to tied rows like RANK(), but it does not skip any ranks after ties. So if two rows tie for rank 1, the next rank will be 2.
Click to reveal answer
beginner
Write a SQL query using ROW_NUMBER() to number employees by salary descending.
SELECT employee_id, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num FROM employees;
Click to reveal answer
intermediate
When should you use DENSE_RANK() instead of RANK()?
Use DENSE_RANK() when you want to rank rows with ties but keep the ranks consecutive without gaps, which is useful for compact ranking lists.
Click to reveal answer
What will ROW_NUMBER() assign to tied rows?
ASame number for all tied rows
BRanks without gaps after ties
CRanks with gaps after ties
DUnique sequential numbers without gaps
If two rows tie for rank 1, what rank does RANK() assign to the next row?
A2
B3
C1
D4
Which function does NOT skip ranks after ties?
AROW_NUMBER()
BRANK()
CDENSE_RANK()
DCOUNT()
Which function assigns unique numbers regardless of ties?
AROW_NUMBER()
BDENSE_RANK()
CRANK()
DSUM()
What is the main use of RANK() in SQL?
ATo assign ranks with gaps after ties
BTo count rows
CTo assign unique row numbers
DTo sum values
Describe the difference between ROW_NUMBER(), RANK(), and DENSE_RANK() with examples.
Think about how each function handles ties and numbering.
You got /4 concepts.
    Explain a real-life scenario where you would use DENSE_RANK() instead of RANK().
    Consider situations where you want a clean list of ranks.
    You got /3 concepts.