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). What values does the second column show for ids 2 and 7? SELECT id, COALESCE(amt, LAG(amt) OVER (ORDER BY id)) FROM Bill;

  1. A. NULL and NULL
  2. B. 300 and 450
  3. C. 300 and NULL
  4. D. 450 and 450

Correct answer: B. 300 and 450

LAG looks at the previous row by id: for id 2 that is bill 1 (300), and for id 7 it is bill 6 (450). COALESCE takes those because the row's own amt is NULL.

More DBMS and SQL questions

Exams that ask this topic