Window Functions · Question 6 · Easy

Table DailySales: day_no | amount 1 | 100 2 | 250 3 | 150 4 | 300 What does this query return (day_no, value)?

SELECT day_no,
       LAG(amount) OVER (ORDER BY day_no)
FROM DailySales
ORDER BY day_no;
  1. 1, 250; 2, 150; 3, 300; 4, NULL
  2. 1, 0; 2, 100; 3, 250; 4, 150
  3. 1, NULL; 2, 100; 3, 250; 4, 150
  4. 1, 100; 2, 100; 3, 250; 4, 150
Answer and explanation

Answer: C. 1, NULL; 2, 100; 3, 250; 4, 150

Explanation: LAG returns the value from the previous row in the window order. Day 1 has no previous row, so it gets NULL (not 0). LEAD would look at the next row instead.

Previous: What does PARTITION BY do inside an OVER clause? · Next: Table Scores: player | team | runs Arjun | Royals | 82 Bhuvan | Royals | 82…

Practise window functions

Try a fresh set of window functions questions with an explanation after every answer, free and without signing up: Window Functions practice. Or sit a full placement mock test.

More window functions questions

More sql practice

NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge