It returns the value of the n-th row in an ordered set of rows within a window or partition.
Click to reveal answer
beginner
How do you specify which row's value NTH_VALUE returns?
You provide the position n as the first argument, like NTH_VALUE(column, n), where n is the row number in the ordered window.
Click to reveal answer
intermediate
What happens if the NTH_VALUE function's n is larger than the number of rows in the window?
The function returns NULL because there is no n-th row to return.
Click to reveal answer
beginner
Why is the ORDER BY clause important when using NTH_VALUE?
Because NTH_VALUE returns the value from the n-th row in the ordered set, the order defines which row is considered first, second, etc.
Click to reveal answer
advanced
Can NTH_VALUE be used without a window frame clause?
Yes, but by default the window frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which may affect results. To get consistent results, specify a frame like ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Click to reveal answer
What does NTH_VALUE(salary, 3) OVER (ORDER BY salary) return?
AThe third lowest salary in the ordered set
BThe third highest salary in the ordered set
CThe salary of the current row
DThe average of the first three salaries
✗ Incorrect
The ORDER BY salary orders salaries ascending by default, so the 3rd row is the third lowest salary.
If the window has only 2 rows, what does NTH_VALUE(column, 5) return?
AThe value of the 2nd row
BNULL
CThe value of the 5th row
DAn error
✗ Incorrect
Since there is no 5th row, the function returns NULL.
Which clause is mandatory to use NTH_VALUE correctly?
AHAVING
BGROUP BY
CORDER BY
DWHERE
✗ Incorrect
ORDER BY defines the row order for NTH_VALUE to pick the n-th row.
What is the default window frame for NTH_VALUE if not specified?
ARANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
BROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
CROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
DNo frame is applied
✗ Incorrect
The default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which can affect results.
Which SQL concept does NTH_VALUE belong to?
AData definition language
BScalar functions
CAggregate functions
DWindow functions
✗ Incorrect
NTH_VALUE is a window function that operates over a set of rows.
Explain how the NTH_VALUE function works and why the ORDER BY clause is important.
Think about how you pick the n-th item from a sorted list.
You got /3 concepts.
Describe what happens if the n in NTH_VALUE(column, n) is larger than the number of rows in the window.
What if you ask for the 10th item but only 5 exist?
You got /3 concepts.
Practice
(1/5)
1. What does the SQL function NTH_VALUE(column, n) do when used with an OVER() clause?
easy
A. Returns the maximum value in the column.
B. Returns the total count of rows in the table.
C. Returns the value at the nth position in the ordered set of rows.
D. Returns the first value in the column without ordering.
Solution
Step 1: Understand the function purpose
The NTH_VALUE function is designed to return a value from a specific position (n) in an ordered set of rows.
Step 2: Recognize the role of OVER()
The OVER() clause defines how rows are ordered and grouped, which is essential for NTH_VALUE to pick the correct nth value.
Final Answer:
Returns the value at the nth position in the ordered set of rows. -> Option C
Quick Check:
NTH_VALUE returns nth value in order [OK]
Hint: NTH_VALUE picks the nth row's value after ordering [OK]
Common Mistakes:
Confusing NTH_VALUE with aggregate functions like COUNT or MAX
Ignoring the ordering inside OVER() clause
Assuming it returns the nth row without ordering
2. Which of the following is the correct syntax to get the 3rd highest salary using NTH_VALUE in SQL?
easy
A. SELECT NTH_VALUE(salary, 3) OVER (PARTITION BY salary) FROM employees;
B. SELECT NTH_VALUE(salary, 3) FROM employees ORDER BY salary DESC;
C. SELECT NTH_VALUE(3, salary) OVER (ORDER BY salary DESC) FROM employees;
D. SELECT NTH_VALUE(salary, 3) OVER (ORDER BY salary DESC) FROM employees;
Solution
Step 1: Check the function parameters
The correct order is NTH_VALUE(column, n), so salary first, then 3.
Step 2: Verify the OVER clause
Ordering by salary descending (ORDER BY salary DESC) ensures the highest salaries come first, so the 3rd highest is the 3rd row.
Final Answer:
SELECT NTH_VALUE(salary, 3) OVER (ORDER BY salary DESC) FROM employees; -> Option D
Quick Check:
Syntax: NTH_VALUE(column, n) OVER (ORDER BY col DESC) [OK]
Hint: NTH_VALUE(column, n) needs OVER with ORDER BY for ranking [OK]
Common Mistakes:
Swapping parameters inside NTH_VALUE
Missing OVER() clause or ORDER BY inside it
Using PARTITION BY incorrectly without ORDER BY
3. Given the table Scores with columns student and score, what will this query return?
SELECT student, score, NTH_VALUE(score, 2) OVER (ORDER BY score DESC) AS second_highest FROM Scores;
medium
A. Each row shows the student's score and the 2nd highest score overall repeated.
B. Each row shows the student's score and their own 2nd highest score.
C. The query returns only the row with the 2nd highest score.
D. The query returns an error because NTH_VALUE needs PARTITION BY.
Solution
Step 1: Understand the window function without PARTITION
Since there is no PARTITION BY, the entire table is treated as one group ordered by score descending.
Step 2: Effect of NTH_VALUE(score, 2) OVER (ORDER BY score DESC)
This returns the 2nd highest score value from the whole table, repeated for every row.
Final Answer:
Each row shows the student's score and the 2nd highest score overall repeated. -> Option A
Quick Check:
NTH_VALUE without PARTITION repeats nth value for all rows [OK]
Hint: Without PARTITION, NTH_VALUE repeats same nth value for all rows [OK]
Common Mistakes:
Thinking NTH_VALUE filters rows to nth only
Assuming PARTITION BY is mandatory
Confusing NTH_VALUE with ROW_NUMBER or RANK
4. Identify the error in this SQL query:
SELECT employee_id, NTH_VALUE(salary, 1) OVER (PARTITION BY department_id) FROM employees;
medium
A. Missing ORDER BY clause inside OVER() causes error.
B. NTH_VALUE cannot be used with PARTITION BY.
C. The function parameters are in wrong order.
D. No error; query is correct.
Solution
Step 1: Check the OVER() clause requirements
NTH_VALUE requires an ORDER BY clause inside OVER() to define the row order for nth value selection.
Step 2: Identify missing ORDER BY
The query uses PARTITION BY but lacks ORDER BY, which causes a syntax error.
Final Answer:
Missing ORDER BY clause inside OVER() causes error. -> Option A
Quick Check:
NTH_VALUE needs ORDER BY in OVER() [OK]
Hint: Always include ORDER BY inside OVER() with NTH_VALUE [OK]
Common Mistakes:
Omitting ORDER BY inside OVER()
Thinking PARTITION BY alone orders rows
Swapping function parameters
5. You want to find the 2nd earliest order date for each customer in the Orders table with columns customer_id and order_date. Which query correctly uses NTH_VALUE to achieve this?
hard
A. SELECT customer_id, order_date, NTH_VALUE(order_date, 2) FROM Orders GROUP BY customer_id;
B. SELECT customer_id, order_date, NTH_VALUE(order_date, 2) OVER (PARTITION BY customer_id ORDER BY order_date ASC) AS second_earliest FROM Orders;
C. SELECT customer_id, order_date, NTH_VALUE(order_date, 2) OVER (ORDER BY customer_id, order_date ASC) AS second_earliest FROM Orders;
D. SELECT customer_id, NTH_VALUE(order_date, 2) FROM Orders ORDER BY order_date ASC;
Solution
Step 1: Use PARTITION BY to group by customer
To find the 2nd earliest order per customer, partition rows by customer_id.
Step 2: Order each partition by order_date ascending
Ordering by order_date ASC ensures earliest dates come first.
Step 3: Apply NTH_VALUE to get 2nd earliest date
NTH_VALUE with n=2 picks the 2nd row's order_date in each partition.
Final Answer:
SELECT customer_id, order_date, NTH_VALUE(order_date, 2) OVER (PARTITION BY customer_id ORDER BY order_date ASC) AS second_earliest FROM Orders; -> Option B
Quick Check:
Partition by customer, order by date ASC, NTH_VALUE(2) [OK]
Hint: Partition by group, order ascending, then NTH_VALUE(n) [OK]
Common Mistakes:
Missing PARTITION BY to group customers
Ordering by wrong columns or descending order
Using NTH_VALUE without OVER() clause
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.