1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. What does COUNT(*) FILTER (WHERE status = 'ok') compute in…

Added 30 Nov -0001

What does COUNT(*) FILTER (WHERE status = 'ok') compute in a grouped query?

  1. An error in most engines, since FILTER applies only to window functions here
  2. The count of distinct status values that equal ok within the group
  3. The number of rows whose status is ok, without needing a CASE expression Correct answer
  4. The total row count, with the ok rows counted twice inside the group

Correct answer

C. The number of rows whose status is ok, without needing a CASE expression

22 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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