CS FundamentalsVery hardTCS Digital · Infosys SP · GATE CS

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 emp, COUNT(*)
FROM Att a
WHERE status = 'P'
  AND NOT EXISTS (SELECT * FROM Att b
                  WHERE b.emp = a.emp AND b.day = a.day - 1
                    AND b.status = 'P')
GROUP BY emp
ORDER BY emp;
  1. A. (u, 5), (v, 3)
  2. B. (u, 1), (v, 1)
  3. C. (u, 2), (v, 1)
  4. D. (u, 2), (v, 2)

Correct answer: D. (u, 2), (v, 2)

A present day with no present day immediately before it starts a streak. u: days 1 and 4 start streaks; v: days 2 and 5. So each has 2 streaks of presence.

More DBMS and SQL questions

Exams that ask this topic