Bird
0
0

Find the issue in this query:

medium📝 Debug Q7 of 15
SQL - Advanced Window Functions
Find the issue in this query:
SELECT id, NTH_VALUE(salary, 3) OVER (ORDER BY salary DESC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS third_salary FROM Employees;
ANTH_VALUE cannot be used with ROWS frame
BThe window frame excludes preceding rows, so 3rd value may not exist
CORDER BY DESC is invalid inside OVER()
DThe query is correct and will return the 3rd highest salary for all rows
Step-by-Step Solution
Solution:
  1. Step 1: Understand window frame boundaries

    ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING includes current and following rows only, excluding preceding rows.
  2. Step 2: Impact on NTH_VALUE

    Since frame starts at current row, for first rows frame may have fewer than 3 rows, so 3rd value may not exist, resulting in NULL.
  3. Final Answer:

    The window frame excludes preceding rows, so 3rd value may not exist -> Option B
  4. Quick Check:

    Window frame affects availability of nth value [OK]
Quick Trick: Window frame must include enough rows for nth value [OK]
Common Mistakes:
  • Assuming NTH_VALUE ignores window frame
  • Thinking ORDER BY DESC is invalid
  • Believing ROWS frame is not allowed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes