Bird
0
0

Find the issue in this query:

medium📝 Debug Q7 of 15
PostgreSQL - Window Functions in PostgreSQL
Find the issue in this query:
SELECT user_id, score, LEAD(score) OVER () AS next_score FROM scores ORDER BY user_id;
ALEAD cannot be used with ORDER BY clause
BLEAD requires a partition clause
CMissing ORDER BY inside OVER() causes undefined row order
DQuery will return syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand OVER() usage

    LEAD without ORDER BY inside OVER() means no defined row order for lead calculation.
  2. Step 2: Check query behavior

    ORDER BY outside OVER() sorts final output but does not define row order for LEAD, causing unpredictable results.
  3. Final Answer:

    Missing ORDER BY inside OVER() causes undefined row order -> Option C
  4. Quick Check:

    LEAD needs ORDER BY inside OVER() for correct next row [OK]
Quick Trick: ORDER BY inside OVER() defines row order for LAG/LEAD [OK]
Common Mistakes:
  • Assuming external ORDER BY affects LEAD order
  • Thinking partition is mandatory
  • Expecting syntax error
  • Ignoring row order importance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes