SELECT COUNT(CASE WHEN status = 'late' THEN 1 END) AS late FROM loans; is executed. How does the result differ from COUNT(*)?
Correct answer
D. It counts only late rows, because the CASE yields NULL for all other statuses
16 views
D. It counts only late rows, because the CASE yields NULL for all other statuses
16 views
Conditional aggregation works because COUNT ignores NULL: the CASE returns 1 only for late rows and NULL otherwise, so the count equals the number of matches. This lets one pass compute several conditional totals, far cheaper than repeated subqueries. SUM(CASE WHEN ... THEN 1 ELSE 0 END) is the equivalent when a zero rather than NULL is preferred.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is D? Ask here and get an explanation.