Challenge - 5 Problems
Top-N Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Top 2 highest salaries per department
Given the Employees table with columns
id, name, department, and salary, which query returns the top 2 highest salaries for each department?SQL
SELECT department, name, salary FROM ( SELECT department, name, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM Employees ) AS ranked WHERE rn <= 2;
Attempts:
2 left
💡 Hint
Use ROW_NUMBER() with PARTITION BY to rank salaries within each department.
✗ Incorrect
Option D uses ROW_NUMBER() window function partitioned by department and ordered by salary descending, then filters to top 2 per group. Other options either use invalid syntax or do not correctly limit per group.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this top-N per group query
Which option contains a syntax error when trying to get the top 3 products by sales per category from a
Products table with columns category, product_name, and sales?SQL
SELECT category, product_name, sales FROM ( SELECT category, product_name, sales, RANK() OVER (PARTITION BY category ORDER BY sales DESC) AS rnk FROM Products ) AS ranked WHERE rnk <= 3;
Attempts:
2 left
💡 Hint
Check if PARTITION BY is allowed in the WHERE or FROM clause.
✗ Incorrect
Option A uses PARTITION BY in the FROM clause without a window function, which is invalid SQL syntax. PARTITION BY is only valid inside window functions like ROW_NUMBER() or RANK().
❓ optimization
advanced2:00remaining
Optimizing top-N per group query for large datasets
You want to retrieve the top 5 orders by amount per customer from a large
Orders table with columns customer_id, order_id, and amount. Which query is the most efficient?Attempts:
2 left
💡 Hint
Window functions with partitioning are usually more efficient than correlated subqueries for top-N per group.
✗ Incorrect
Option B uses ROW_NUMBER() window function which is optimized for large datasets and avoids correlated subqueries. Options B and D use correlated subqueries which are less efficient. Option B does not limit per group.
🧠 Conceptual
advanced2:00remaining
Understanding difference between RANK() and ROW_NUMBER() in top-N queries
Which statement correctly explains the difference between
RANK() and ROW_NUMBER() when used to get top-N rows per group?Attempts:
2 left
💡 Hint
Think about how ties are handled in ranking functions.
✗ Incorrect
ROW_NUMBER() gives a unique number to each row, even if values tie, so no gaps. RANK() gives the same rank to tied rows but skips ranks after ties, causing gaps.🔧 Debug
expert3:00remaining
Why does this top-N per group query return fewer rows than expected?
You run this query to get the top 3 employees by salary per department from
Employees table, but some departments return fewer than 3 rows. Why?
SELECT department, name, salary
FROM (
SELECT department, name, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS drnk
FROM Employees
) AS ranked
WHERE drnk <= 3;Attempts:
2 left
💡 Hint
Consider how DENSE_RANK() handles ties and how that affects the number of rows returned.
✗ Incorrect
DENSE_RANK() assigns the same rank to tied salaries without gaps. If many employees share the same salary at a rank, the count of rows with ranks <= 3 may be less than 3 per department.