Bird
0
0

You want to group sales by Region but treat NULL regions as 'Unknown'. Which query correctly does this?

hard📝 Application Q8 of 15
SQL - GROUP BY and HAVING
You want to group sales by Region but treat NULL regions as 'Unknown'. Which query correctly does this?
ASELECT Region, SUM(Sales) FROM SalesData GROUP BY Region HAVING Region IS NOT NULL;
BSELECT Region, SUM(Sales) FROM SalesData GROUP BY Region WHERE Region IS NOT NULL;
CSELECT COALESCE(Region, 'Unknown') AS RegionGroup, SUM(Sales) FROM SalesData GROUP BY COALESCE(Region, 'Unknown');
DSELECT Region, SUM(Sales) FROM SalesData GROUP BY Region NULLS FIRST;
Step-by-Step Solution
Solution:
  1. Step 1: Replace NULL with 'Unknown' using COALESCE

    COALESCE(Region, 'Unknown') converts NULLs to 'Unknown'.
  2. Step 2: Group by the COALESCE expression

    Grouping by COALESCE groups NULLs under 'Unknown'.
  3. Final Answer:

    SELECT COALESCE(Region, 'Unknown') AS RegionGroup, SUM(Sales) FROM SalesData GROUP BY COALESCE(Region, 'Unknown'); -> Option C
  4. Quick Check:

    Use COALESCE to group NULLs as 'Unknown' [OK]
Quick Trick: Use COALESCE to replace NULLs before grouping [OK]
Common Mistakes:
MISTAKES
  • Filtering out NULLs instead of replacing
  • Using WHERE or HAVING incorrectly
  • Assuming GROUP BY NULLS FIRST changes grouping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes