What does COUNT(*) FILTER (WHERE status = 'ok') compute in a grouped query?
Correct answer
C. The number of rows whose status is ok, without needing a CASE expression
22 views
C. The number of rows whose status is ok, without needing a CASE expression
22 views
The FILTER clause restricts which rows an aggregate sees, so COUNT(*) FILTER (WHERE status='ok') is the standard-SQL equivalent of SUM(CASE WHEN status='ok' THEN 1 ELSE 0 END). It keeps an aggregate list readable when several conditional counts share one GROUP BY. PostgreSQL and SQLite support it directly, while MySQL emulates it with CASE or SUM over a boolean.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is C? Ask here and get an explanation.