Window Functions · Question 9 · Medium

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

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

Answer: A. 1, NULL; 2, 150; 3, -100; 4, 150

Explanation: Each row subtracts the previous day's amount: 250 - 100 = 150, 150 - 250 = -100, 300 - 150 = 150. Day 1 has no previous row, LAG gives NULL, and 100 - NULL is NULL.

Previous: Table Results: student | section | marks Aditi | A | 92 Bharti | A | 92 Chetna… · Next: Table Wallet: txn_id | holder | amount 1 | Tara | 500 2 | Uday | 200 3 | Tara…

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