0
0
PostgreSQLquery~20 mins

ROW_NUMBER, RANK, DENSE_RANK in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ranking Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Difference between ROW_NUMBER and RANK

Consider a table sales with columns id, employee, and amount. The data is:

id | employee | amount
---+----------+-------
1  | Alice    | 100
2  | Bob      | 200
3  | Alice    | 200
4  | Bob      | 200
5  | Carol    | 300

What is the output of this query?

SELECT employee, amount, ROW_NUMBER() OVER (PARTITION BY amount ORDER BY employee) AS rn
FROM sales
ORDER BY amount, employee;
PostgreSQL
SELECT employee, amount, ROW_NUMBER() OVER (PARTITION BY amount ORDER BY employee) AS rn
FROM sales
ORDER BY amount, employee;
A[{"employee": "Alice", "amount": 100, "rn": 1}, {"employee": "Bob", "amount": 200, "rn": 1}, {"employee": "Alice", "amount": 200, "rn": 2}, {"employee": "Bob", "amount": 200, "rn": 3}, {"employee": "Carol", "amount": 300, "rn": 1}]
B[{"employee": "Alice", "amount": 100, "rn": 1}, {"employee": "Bob", "amount": 200, "rn": 1}, {"employee": "Bob", "amount": 200, "rn": 2}, {"employee": "Alice", "amount": 200, "rn": 3}, {"employee": "Carol", "amount": 300, "rn": 1}]
C[{"employee": "Alice", "amount": 100, "rn": 1}, {"employee": "Alice", "amount": 200, "rn": 1}, {"employee": "Bob", "amount": 200, "rn": 2}, {"employee": "Bob", "amount": 200, "rn": 3}, {"employee": "Carol", "amount": 300, "rn": 2}]
D[{"employee": "Alice", "amount": 100, "rn": 1}, {"employee": "Alice", "amount": 200, "rn": 1}, {"employee": "Bob", "amount": 200, "rn": 2}, {"employee": "Bob", "amount": 200, "rn": 3}, {"employee": "Carol", "amount": 300, "rn": 1}]
Attempts:
2 left
💡 Hint

ROW_NUMBER assigns a unique number to each row within the partition, ordered by the specified column.

query_result
intermediate
2:00remaining
Understanding RANK with ties

Using the same sales table, what is the output of this query?

SELECT employee, amount, RANK() OVER (ORDER BY amount DESC) AS rnk
FROM sales
ORDER BY amount DESC, employee;
PostgreSQL
SELECT employee, amount, RANK() OVER (ORDER BY amount DESC) AS rnk
FROM sales
ORDER BY amount DESC, employee;
A[{"employee": "Carol", "amount": 300, "rnk": 1}, {"employee": "Alice", "amount": 200, "rnk": 2}, {"employee": "Bob", "amount": 200, "rnk": 3}, {"employee": "Bob", "amount": 200, "rnk": 4}, {"employee": "Alice", "amount": 100, "rnk": 5}]
B[{"employee": "Carol", "amount": 300, "rnk": 1}, {"employee": "Alice", "amount": 200, "rnk": 2}, {"employee": "Bob", "amount": 200, "rnk": 2}, {"employee": "Bob", "amount": 200, "rnk": 2}, {"employee": "Alice", "amount": 100, "rnk": 5}]
C[{"employee": "Carol", "amount": 300, "rnk": 1}, {"employee": "Alice", "amount": 200, "rnk": 2}, {"employee": "Bob", "amount": 200, "rnk": 2}, {"employee": "Bob", "amount": 200, "rnk": 3}, {"employee": "Alice", "amount": 100, "rnk": 4}]
D[{"employee": "Carol", "amount": 300, "rnk": 1}, {"employee": "Alice", "amount": 200, "rnk": 2}, {"employee": "Bob", "amount": 200, "rnk": 2}, {"employee": "Bob", "amount": 200, "rnk": 2}, {"employee": "Alice", "amount": 100, "rnk": 4}]
Attempts:
2 left
💡 Hint

RANK() assigns the same rank to ties and skips ranks after ties.

query_result
advanced
2:00remaining
DENSE_RANK behavior with duplicates

Using the same sales table, what is the output of this query?

SELECT employee, amount, DENSE_RANK() OVER (ORDER BY amount DESC) AS drnk
FROM sales
ORDER BY amount DESC, employee;
PostgreSQL
SELECT employee, amount, DENSE_RANK() OVER (ORDER BY amount DESC) AS drnk
FROM sales
ORDER BY amount DESC, employee;
A[{"employee": "Carol", "amount": 300, "drnk": 1}, {"employee": "Alice", "amount": 200, "drnk": 2}, {"employee": "Bob", "amount": 200, "drnk": 2}, {"employee": "Bob", "amount": 200, "drnk": 2}, {"employee": "Alice", "amount": 100, "drnk": 3}]
B[{"employee": "Carol", "amount": 300, "drnk": 1}, {"employee": "Alice", "amount": 200, "drnk": 2}, {"employee": "Bob", "amount": 200, "drnk": 3}, {"employee": "Bob", "amount": 200, "drnk": 4}, {"employee": "Alice", "amount": 100, "drnk": 5}]
C[{"employee": "Carol", "amount": 300, "drnk": 1}, {"employee": "Alice", "amount": 200, "drnk": 2}, {"employee": "Bob", "amount": 200, "drnk": 3}, {"employee": "Bob", "amount": 200, "drnk": 3}, {"employee": "Alice", "amount": 100, "drnk": 4}]
D[{"employee": "Carol", "amount": 300, "drnk": 1}, {"employee": "Alice", "amount": 200, "drnk": 1}, {"employee": "Bob", "amount": 200, "drnk": 2}, {"employee": "Bob", "amount": 200, "drnk": 2}, {"employee": "Alice", "amount": 100, "drnk": 3}]
Attempts:
2 left
💡 Hint

DENSE_RANK() assigns ranks without gaps after ties.

🧠 Conceptual
advanced
1:30remaining
Choosing the right ranking function

You want to assign a rank to sales amounts where ties get the same rank and the next rank number should not skip any numbers. Which function should you use?

AROW_NUMBER()
BRANK()
CDENSE_RANK()
DCOUNT()
Attempts:
2 left
💡 Hint

Think about which function assigns ranks without gaps after ties.

📝 Syntax
expert
1:30remaining
Identify the syntax error in window function usage

Which option contains a syntax error in this window function query?

SELECT employee, amount, RANK() OVER PARTITION BY amount ORDER BY employee) AS rnk FROM sales;
ASELECT employee, amount, RANK() OVER PARTITION BY amount ORDER BY employee AS rnk FROM sales;
BSELECT employee, amount, RANK() OVER (PARTITION BY amount ORDER BY employee) rnk FROM sales;
CSELECT employee, amount, RANK() OVER (PARTITION BY amount ORDER BY employee) AS rnk FROM sales;
DSELECT employee, amount, RANK() OVER (PARTITION BY amount ORDER BY employee) AS rnk FROM sales WHERE amount > 100;
Attempts:
2 left
💡 Hint

Check the parentheses and placement of the OVER clause.