Bird
0
0

Find the issue in this query:

medium📝 Debug Q7 of 15
SQL - CASE Expressions
Find the issue in this query:
SELECT department, SUM(CASE WHEN salary > 40000 THEN salary ELSE 0 END) AS total_salary FROM employees GROUP BY department HAVING total_salary > 100000;
AELSE 0 should be ELSE NULL
BSUM cannot be used with CASE
CAlias total_salary cannot be used in HAVING clause directly
DGROUP BY is missing department column
Step-by-Step Solution
Solution:
  1. Step 1: Understand HAVING clause usage

    HAVING filters groups but cannot use column aliases directly in many SQL dialects.
  2. Step 2: Identify correct HAVING usage

    Must repeat the aggregate expression or use a subquery to filter by total_salary.
  3. Final Answer:

    Alias total_salary cannot be used in HAVING clause directly -> Option C
  4. Quick Check:

    HAVING needs full expression or subquery [OK]
Quick Trick: Use full aggregate in HAVING, not alias [OK]
Common Mistakes:
  • Using alias in HAVING directly
  • Thinking SUM disallows CASE
  • Confusing ELSE 0 with ELSE NULL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes