Bird
0
0

Given the table Sales with columns id and amount, what will this query return?

medium📝 query result Q4 of 15
SQL - Advanced Window Functions
Given the table Sales with columns id and amount, what will this query return?
SELECT id, amount, LAG(amount) OVER (ORDER BY id) AS prev_amount FROM Sales;
AEach row shows its amount and the amount from the previous row ordered by id
BEach row shows its amount and the amount from the next row ordered by id
CEach row shows the sum of current and previous amounts
DEach row shows only the current amount without previous data
Step-by-Step Solution
Solution:
  1. Step 1: Understand LAG with ORDER BY

    The query uses LAG(amount) to get the previous row's amount based on id order.
  2. Step 2: Interpret the output

    Each row will have its own amount and the amount from the row before it in id order; the first row's previous amount will be NULL.
  3. Final Answer:

    Each row shows its amount and the amount from the previous row ordered by id -> Option A
  4. Quick Check:

    LAG returns previous row value in order [OK]
Quick Trick: LAG returns previous row's value in ORDER BY sequence [OK]
Common Mistakes:
  • Thinking LAG returns next row's value
  • Assuming LAG sums values
  • Ignoring NULL for first row

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes