CS FundamentalsHardGATE CS

Sa(m, amt) holds monthly sales (2024-01, 100), (2024-02, 130), (2024-03, 130), (2024-04, 90), (2024-05, 150), (2024-06, 150), (2024-07, 120) (m is a sortable month string). How many rows does SELECT amt FROM (SELECT amt, LAG(amt) OVER (ORDER BY m) AS p FROM Sa) WHERE amt > p return?

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

Correct answer: B. 2

LAG gives the previous month's amount (NULL for the first row, so its comparison is UNKNOWN and the row is dropped). Only months 02 and 05 show a strict increase, so 2 rows are returned; using >= would add the repeated months 03 and 06 (4 rows).

More DBMS and SQL questions

Exams that ask this topic