Challenge - 5 Problems
Master of GROUP BY with NULLs
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of GROUP BY with NULL values
Consider the table Employees with columns
What is the output of this query?
Department and Salary. Some Department values are NULL.What is the output of this query?
SELECT Department, COUNT(*) AS Count FROM Employees GROUP BY Department ORDER BY Department;
SQL
CREATE TABLE Employees (Department VARCHAR(20), Salary INT); INSERT INTO Employees VALUES (NULL, 5000), ('HR', 4000), ('IT', 6000), (NULL, 4500), ('HR', 4200);
Attempts:
2 left
💡 Hint
Remember that NULL values are grouped together as one group in GROUP BY.
✗ Incorrect
In SQL, NULL values in GROUP BY are treated as a single group. So all rows with Department NULL are counted together.
🧠 Conceptual
intermediate1:30remaining
How does GROUP BY treat NULL values?
In SQL, when you use GROUP BY on a column that contains NULL values, how are those NULL values treated?
Attempts:
2 left
💡 Hint
Think about how NULL represents unknown but equal unknowns in grouping.
✗ Incorrect
SQL treats all NULLs in a GROUP BY column as one group, so they are counted together.
📝 Syntax
advanced2:00remaining
Identify the error in GROUP BY with NULL handling
Which of the following SQL queries will cause a syntax error when grouping by a column that contains NULL values?
SQL
Table: Sales (Region VARCHAR, Amount INT) with some NULL Region values
Attempts:
2 left
💡 Hint
Remember the order of clauses in SQL SELECT statements.
✗ Incorrect
The WHERE clause cannot come after GROUP BY; it must come before. Using WHERE after GROUP BY causes syntax error.
❓ optimization
advanced2:30remaining
Optimizing GROUP BY with NULL values filtering
You want to count rows per category but exclude NULL categories efficiently. Which query is best for performance?
Attempts:
2 left
💡 Hint
Filtering rows before grouping reduces data processed.
✗ Incorrect
Filtering NULLs in WHERE clause reduces rows before grouping, improving performance over HAVING.
🔧 Debug
expert3:00remaining
Why does this GROUP BY query return unexpected NULL group?
Given the table
What is the most likely reason for this NULL group appearing?
Orders with columns CustomerID and Status, the query below returns a group with NULL CustomerID unexpectedly.SELECT CustomerID, COUNT(*) FROM Orders GROUP BY CustomerID;
What is the most likely reason for this NULL group appearing?
Attempts:
2 left
💡 Hint
Check if any rows have NULL in the grouped column.
✗ Incorrect
GROUP BY groups all NULL values together, so if CustomerID is NULL in some rows, they form a NULL group.