CS FundamentalsHardGATE CS · Amazon/Microsoft SDE MCQ

Table Bill(id, cust, region, amt) rows: (1, 'Kavi', 'E', 300), (2, 'Lata', 'W', NULL), (3, 'Kavi', 'E', 450), (4, 'Mona', 'W', 300), (5, 'Nair', NULL, 200), (6, 'Lata', 'W', 450), (7, 'Mona', 'E', NULL). How many NULLs appear in the column diff? SELECT id, amt - LAG(amt) OVER (ORDER BY id) AS diff FROM Bill;

  1. A. 2
  2. B. 3
  3. C. 5
  4. D. 4

Correct answer: D. 4

diff is NULL for id 1 (no previous row), id 2 (own amt NULL), id 3 (previous amt NULL) and id 7 (own amt NULL); ids 4, 5 and 6 give -150, -100 and 250. That is 4 NULLs.

More DBMS and SQL questions

Exams that ask this topic