0
0
SQLquery~5 mins

NTH_VALUE function in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the NTH_VALUE function do in SQL?
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
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
Which clause is mandatory to use NTH_VALUE correctly?
AHAVING
BGROUP BY
CORDER BY
DWHERE
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
Which SQL concept does NTH_VALUE belong to?
AData definition language
BScalar functions
CAggregate functions
DWindow functions
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.