0
0
SQLquery~20 mins

GROUP BY with NULL values behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2:00remaining
Output of GROUP BY with NULL values
Consider the table Employees with columns 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);
A[{"Department": "HR", "Count": 2}, {"Department": "IT", "Count": 1}, {"Department": null, "Count": 0}]
B[{"Department": "HR", "Count": 2}, {"Department": "IT", "Count": 1}]
C[{"Department": null, "Count": 2}, {"Department": "HR", "Count": 2}, {"Department": "IT", "Count": 1}]
D[{"Department": "HR", "Count": 2}, {"Department": "IT", "Count": 1}, {"Department": "NULL", "Count": 2}]
Attempts:
2 left
💡 Hint
Remember that NULL values are grouped together as one group in GROUP BY.
🧠 Conceptual
intermediate
1: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?
ANULL values are ignored and not included in any group.
BEach NULL value is treated as a distinct group.
CNULL values cause the query to fail with an error.
DAll NULL values are grouped together as a single group.
Attempts:
2 left
💡 Hint
Think about how NULL represents unknown but equal unknowns in grouping.
📝 Syntax
advanced
2: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
ASELECT Region, SUM(Amount) FROM Sales GROUP BY Region;
BSELECT Region, SUM(Amount) FROM Sales GROUP BY Region WHERE Region IS NOT NULL;
CSELECT Region, SUM(Amount) FROM Sales GROUP BY Region HAVING Region IS NOT NULL;
DSELECT Region, SUM(Amount) FROM Sales GROUP BY Region ORDER BY Region;
Attempts:
2 left
💡 Hint
Remember the order of clauses in SQL SELECT statements.
optimization
advanced
2: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?
ASELECT Category, COUNT(*) FROM Products WHERE Category IS NOT NULL GROUP BY Category;
BSELECT Category, COUNT(*) FROM Products GROUP BY Category HAVING Category IS NOT NULL;
CSELECT Category, COUNT(*) FROM Products GROUP BY Category;
DSELECT Category, COUNT(*) FROM Products WHERE Category <> NULL GROUP BY Category;
Attempts:
2 left
💡 Hint
Filtering rows before grouping reduces data processed.
🔧 Debug
expert
3:00remaining
Why does this GROUP BY query return unexpected NULL group?
Given the table 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?
AThere are rows where CustomerID is NULL, and GROUP BY treats all NULLs as one group.
BThe query is missing a WHERE clause to exclude NULL CustomerIDs, causing a syntax error.
CCustomerID column has a default value of NULL which is not allowed in GROUP BY.
DThe database automatically converts NULLs to zero in GROUP BY, causing confusion.
Attempts:
2 left
💡 Hint
Check if any rows have NULL in the grouped column.