CS FundamentalsModerateGATE CS (PYQ pattern)

LG(g, t, v): (a,1,10), (a,2,15), (a,3,12), (b,1,7), (b,2,7), (b,3,20). What does SELECT SUM(d) FROM (SELECT v - COALESCE(LAG(v) OVER (PARTITION BY g ORDER BY t), v) AS d FROM LG) x return?

  1. A. 10
  2. B. 22
  3. C. 15
  4. D. 0

Correct answer: C. 15

Within each partition the first row has no LAG, so COALESCE makes its difference 0. Group a: 0 + 5 - 3 = 2; group b: 0 + 0 + 13 = 13. Total 15. Without PARTITION BY the first b row would be compared with the last a row (giving 10).

More DBMS and SQL questions

Exams that ask this topic