Bird
0
0

Which SQL statement correctly uses LAG to retrieve the previous score ordered by player_id?

easy📝 Syntax Q3 of 15
SQL - Advanced Window Functions
Which SQL statement correctly uses LAG to retrieve the previous score ordered by player_id?
ASELECT player_id, score, LAG(score) OVER (ORDER BY player_id) AS prev_score FROM Players;
BSELECT player_id, score, LAG(score ORDER BY player_id) AS prev_score FROM Players;
CSELECT player_id, score, LAG(score) AS prev_score ORDER BY player_id FROM Players;
DSELECT player_id, score, LAG(score, 1) FROM Players ORDER BY player_id;
Step-by-Step Solution
Solution:
  1. Step 1: Recall LAG syntax

    LAG(column) OVER (ORDER BY column) is the correct syntax to get previous row's value ordered properly.
  2. Step 2: Analyze options

    SELECT player_id, score, LAG(score) OVER (ORDER BY player_id) AS prev_score FROM Players; correctly uses LAG with OVER and ORDER BY inside parentheses.
    SELECT player_id, score, LAG(score ORDER BY player_id) AS prev_score FROM Players; misplaces ORDER BY inside LAG.
    SELECT player_id, score, LAG(score) AS prev_score ORDER BY player_id FROM Players; places ORDER BY outside window function.
    SELECT player_id, score, LAG(score, 1) FROM Players ORDER BY player_id; orders the entire query but misses OVER clause.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    LAG requires OVER with ORDER BY inside [OK]
Quick Trick: LAG needs OVER(ORDER BY ...) clause [OK]
Common Mistakes:
  • Placing ORDER BY outside OVER clause
  • Omitting OVER clause
  • Misplacing ORDER BY inside LAG parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes