Bird
0
0

Which of the following is the correct syntax to count rows where score is greater than 50 using CASE inside COUNT?

easy📝 Syntax Q12 of 15
SQL - CASE Expressions
Which of the following is the correct syntax to count rows where score is greater than 50 using CASE inside COUNT?
ASELECT COUNT(CASE WHEN score > 50 THEN 1 END) FROM results;
BSELECT COUNT(CASE score > 50 THEN 1 ELSE 0 END) FROM results;
CSELECT COUNT(CASE WHEN score > 50 THEN 1 ELSE 0 END) FROM results;
DSELECT COUNT(CASE score > 50 THEN 1 END) FROM results;
Step-by-Step Solution
Solution:
  1. Step 1: Recall COUNT counts non-null values

    COUNT counts rows where expression is NOT NULL.
  2. Step 2: Use CASE to return 1 for matches, NULL otherwise

    CASE WHEN condition THEN 1 END returns 1 or NULL (default ELSE NULL).
  3. Final Answer:

    SELECT COUNT(CASE WHEN score > 50 THEN 1 END) FROM results; -> Option A
  4. Quick Check:

    COUNT with CASE returning NULL for no match counts only matches [OK]
Quick Trick: COUNT counts non-null, so ELSE NULL is default [OK]
Common Mistakes:
  • Using ELSE 0 causes COUNT to count all rows
  • Omitting WHEN keyword
  • Using invalid CASE syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes