0
0
SQLquery~20 mins

Aggregate with NULL handling in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Aggregate NULL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this query with NULLs in aggregation?
Consider a table sales with columns 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);
A[{"region": "North", "total_sales": 100}, {"region": "South", "total_sales": 200}]
B[{"region": "North", "total_sales": 100}, {"region": "South", "total_sales": 200}, {"region": "East", "total_sales": 0}]
C[{"region": "North", "total_sales": 100}, {"region": "South", "total_sales": 200}, {"region": "East", "total_sales": null}]
D[{"region": "North", "total_sales": null}, {"region": "South", "total_sales": null}, {"region": "East", "total_sales": null}]
Attempts:
2 left
💡 Hint
SUM ignores NULL values and returns NULL only if all values are NULL for that group.
query_result
intermediate
2: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);
A[4, 2]
B[2, 4]
C[4, 4]
D[2, 2]
Attempts:
2 left
💡 Hint
COUNT(*) counts all rows, COUNT(column) counts only non-NULL values in that column.
📝 Syntax
advanced
2: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?
ASELECT SUM(score) WHERE score IS NOT NULL OR 0 FROM results;
BSELECT SUM(NULLIF(score, 0)) FROM results;
CSELECT SUM(IFNULL(score)) FROM results;
DSELECT SUM(COALESCE(score, 0)) FROM results;
Attempts:
2 left
💡 Hint
Use a function that replaces NULL with a value before summing.
🔧 Debug
advanced
2:00remaining
Why does this query return NULL instead of zero?
Given this query:
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);
ANo rows satisfy discount > 0, so AVG returns NULL.
BThe WHERE clause is invalid and causes NULL result.
CAVG returns NULL if any discount is NULL in the table.
DAVG cannot be used with NULL values in any rows.
Attempts:
2 left
💡 Hint
Check how many rows meet the WHERE condition.
🧠 Conceptual
expert
2: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?
ASUM(DISTINCT category)
BCOUNT(DISTINCT category)
CCOUNT(category)
DCOUNT(*)
Attempts:
2 left
💡 Hint
Think about counting unique values excluding NULLs.