Bird
0
0

Identify the error in this SQL query:

medium📝 Debug Q14 of 15
SQL - Advanced Window Functions
Identify the error in this SQL query:
SELECT employee_id, NTH_VALUE(salary, 1) OVER (PARTITION BY department_id) FROM employees;
AMissing ORDER BY clause inside OVER() causes error.
BNTH_VALUE cannot be used with PARTITION BY.
CThe function parameters are in wrong order.
DNo error; query is correct.
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Identify missing ORDER BY

    The query uses PARTITION BY but lacks ORDER BY, which causes a syntax error.
  3. Final Answer:

    Missing ORDER BY clause inside OVER() causes error. -> Option A
  4. Quick Check:

    NTH_VALUE needs ORDER BY in OVER() [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes