1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. SELECT COUNT(CASE WHEN status = 'late' THEN 1 END) AS late…

Added 30 Nov -0001

SELECT COUNT(CASE WHEN status = 'late' THEN 1 END) AS late FROM loans; is executed. How does the result differ from COUNT(*)?

  1. It counts every row, because COUNT ignores the NULL result of the CASE expression
  2. It counts late rows twice, since the CASE duplicates every row that matches
  3. It fails, because COUNT cannot accept a CASE expression as its only argument
  4. It counts only late rows, because the CASE yields NULL for all other statuses Correct answer

Correct answer

D. It counts only late rows, because the CASE yields NULL for all other statuses

16 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs