Challenge - 5 Problems
NTH_VALUE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of NTH_VALUE with simple partition
Given the table sales with columns
region and amount, what is the output of the following query?SELECT region, amount, NTH_VALUE(amount, 2) OVER (PARTITION BY region ORDER BY amount ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS second_amount FROM sales;
SQL
CREATE TABLE sales (region VARCHAR(10), amount INT); INSERT INTO sales VALUES ('East', 100), ('East', 200), ('East', 300), ('West', 150), ('West', 250); SELECT region, amount, NTH_VALUE(amount, 2) OVER (PARTITION BY region ORDER BY amount ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS second_amount FROM sales ORDER BY region, amount;
Attempts:
2 left
💡 Hint
Remember that NTH_VALUE returns the value of the nth row in the window frame, ordered by the ORDER BY clause.
✗ Incorrect
The NTH_VALUE(amount, 2) function returns the second smallest amount within each region partition. For 'East', the amounts ordered are 100, 200, 300, so the second is 200. For 'West', the amounts are 150, 250, so the second is 250. This value is repeated for all rows in the partition.
🧠 Conceptual
intermediate2:00remaining
Understanding frame specification impact on NTH_VALUE
What happens if you run the query below on a table
Which statement is true?
orders with columns customer_id and order_date? Assume multiple orders per customer.SELECT customer_id, order_date, NTH_VALUE(order_date, 3) OVER (PARTITION BY customer_id ORDER BY order_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS third_order_date FROM orders;
Which statement is true?
Attempts:
2 left
💡 Hint
Consider how the frame defined by ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW changes as the window moves.
✗ Incorrect
The frame grows from the first row to the current row. For the first two rows, the frame has fewer than three rows, so NTH_VALUE returns NULL. Starting from the third row, the frame includes at least three rows, so the third order date in the frame is returned.
📝 Syntax
advanced2:00remaining
Identify the syntax error in NTH_VALUE usage
Which of the following queries will cause a syntax error?
Attempts:
2 left
💡 Hint
Check the valid range for the second argument of NTH_VALUE.
✗ Incorrect
The second argument to NTH_VALUE must be a positive integer starting from 1. Using 0 is invalid and causes a syntax error.
❓ optimization
advanced2:00remaining
Optimizing NTH_VALUE with large datasets
You have a large
transactions table with millions of rows. You want to find the 5th highest transaction amount per customer using NTH_VALUE. Which approach is most efficient?Attempts:
2 left
💡 Hint
Consider performance implications of window functions on large data and alternatives.
✗ Incorrect
Using ROW_NUMBER() in a subquery to filter the 5th row per customer is often more efficient than NTH_VALUE on large datasets because it avoids computing the window function for all rows and allows filtering early.
🔧 Debug
expert3:00remaining
Debugging unexpected NULLs from NTH_VALUE
You run this query:
But you notice that
SELECT employee_id, salary, NTH_VALUE(salary, 3) OVER (PARTITION BY department ORDER BY salary) AS third_salary FROM employees;
But you notice that
third_salary is NULL for some employees even though their department has more than three employees. What is the most likely cause?Attempts:
2 left
💡 Hint
Think about how the default frame affects which rows are visible to the function at each row.
✗ Incorrect
By default, the frame is from the first row to the current row. For the first and second rows, the frame has fewer than three rows, so NTH_VALUE returns NULL. Only from the third row onward does the frame include the third row, returning a non-NULL value.