Challenge - 5 Problems
Aggregate NULL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the result of this query with NULLs in aggregation?
Consider a table sales with columns
What is the output of this query?
region and amount. Some amount values are NULL.What is the output of this query?
SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region;
SQL
CREATE TABLE sales (region VARCHAR(10), amount INT); INSERT INTO sales VALUES ('North', 100), ('North', NULL), ('South', 200), ('South', NULL), ('East', NULL);
Attempts:
2 left
💡 Hint
SUM ignores NULL values and returns NULL only if all values are NULL for that group.
✗ Incorrect
The SUM aggregate function skips NULLs. For 'North' and 'South', there are non-NULL amounts, so sums are 100 and 200 respectively. For 'East', all amounts are NULL, so SUM returns NULL.
❓ query_result
intermediate2:00remaining
How does COUNT(*) differ from COUNT(column) with NULLs?
Given a table
employees with a column manager_id that can be NULL, what is the output of this query?SELECT COUNT(*), COUNT(manager_id) FROM employees;
SQL
CREATE TABLE employees (id INT, manager_id INT); INSERT INTO employees VALUES (1, NULL), (2, 1), (3, NULL), (4, 2);
Attempts:
2 left
💡 Hint
COUNT(*) counts all rows, COUNT(column) counts only non-NULL values in that column.
✗ Incorrect
COUNT(*) counts all 4 rows. COUNT(manager_id) counts only rows where manager_id is not NULL, which are 2 rows.
📝 Syntax
advanced2:00remaining
Which query correctly replaces NULLs with zero before aggregation?
You want to sum the
score column but treat NULL as zero. Which query is correct?Attempts:
2 left
💡 Hint
Use a function that replaces NULL with a value before summing.
✗ Incorrect
COALESCE(score, 0) replaces NULL with 0, so SUM adds zeros instead of ignoring NULLs. NULLIF returns NULL if score=0, which is not desired. IFNULL requires two arguments. The WHERE clause syntax is invalid.
🔧 Debug
advanced2:00remaining
Why does this query return NULL instead of zero?
Given this query:
Some rows have NULL in discount. The result is NULL. Why?
SELECT AVG(discount) FROM orders WHERE discount > 0;
Some rows have NULL in discount. The result is NULL. Why?
SQL
CREATE TABLE orders (id INT, discount INT); INSERT INTO orders VALUES (1, NULL), (2, 0), (3, NULL);
Attempts:
2 left
💡 Hint
Check how many rows meet the WHERE condition.
✗ Incorrect
The WHERE discount > 0 filters out all rows because none have discount > 0. AVG of zero rows returns NULL.
🧠 Conceptual
expert2:00remaining
Which aggregate function returns the count of distinct non-NULL values?
You want to count how many unique non-NULL values exist in a column
category. Which aggregate function achieves this?Attempts:
2 left
💡 Hint
Think about counting unique values excluding NULLs.
✗ Incorrect
COUNT(DISTINCT category) counts unique non-NULL values. SUM(DISTINCT category) is invalid for non-numeric or may sum distinct values but not count them. COUNT(category) counts all non-NULL values including duplicates. COUNT(*) counts all rows.