Bird
0
0

Which of the following is the correct syntax to get the 3rd highest salary using NTH_VALUE in SQL?

easy📝 Syntax Q12 of 15
SQL - Advanced Window Functions
Which of the following is the correct syntax to get the 3rd highest salary using NTH_VALUE in SQL?
ASELECT NTH_VALUE(salary, 3) OVER (PARTITION BY salary) FROM employees;
BSELECT NTH_VALUE(salary, 3) FROM employees ORDER BY salary DESC;
CSELECT NTH_VALUE(3, salary) OVER (ORDER BY salary DESC) FROM employees;
DSELECT NTH_VALUE(salary, 3) OVER (ORDER BY salary DESC) FROM employees;
Step-by-Step Solution
Solution:
  1. Step 1: Check the function parameters

    The correct order is NTH_VALUE(column, n), so salary first, then 3.
  2. 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.
  3. Final Answer:

    SELECT NTH_VALUE(salary, 3) OVER (ORDER BY salary DESC) FROM employees; -> Option D
  4. Quick Check:

    Syntax: NTH_VALUE(column, n) OVER (ORDER BY col DESC) [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes