CS FundamentalsVery hardGATE CS · SQL

Table Att(emp, day, status) rows: ('u', 1, 'P'), ('u', 2, 'P'), ('u', 3, 'A'), ('u', 4, 'P'), ('u', 5, 'P'), ('u', 6, 'P'), ('v', 1, 'A'), ('v', 2, 'P'), ('v', 3, 'P'), ('v', 4, 'A'), ('v', 5, 'P'). What does this query return?

Table Att(emp, day, status) rows: ('u', 1, 'P'), ('u', 2, 'P'), ('u', 3, 'A'), ('u', 4, 'P'), ('u', 5, 'P'), ('u', 6, 'P'), ('v', 1, 'A'), ('v', 2, 'P'), ('v', 3, 'P'), ('v', 4, 'A'), ('v', 5, 'P'). What does this query return?
SELECT COUNT(*) FROM (
  SELECT status,
         LAG(status) OVER (PARTITION BY emp ORDER BY day) AS prev
  FROM Att) x
WHERE status <> prev;
  1. A. 5
  2. B. 7
  3. C. 4
  4. D. 6

Correct answer: A. 5

u's sequence P P A P P P changes twice (day 3 and day 4); v's A P P A P changes three times (days 2, 4, 5). Each employee's first row has prev = NULL, so status <> NULL is unknown and not counted: 5.

More DBMS and SQL questions

Exams that ask this topic