Challenge - 5 Problems
COUNT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
COUNT with NULL values
Given the table Employees with a column
ManagerID that can contain NULL values, what will be the result of this query?SELECT COUNT(ManagerID) FROM Employees;
SQL
SELECT COUNT(ManagerID) FROM Employees;
Attempts:
2 left
💡 Hint
Remember that COUNT(column) ignores NULL values in that column.
✗ Incorrect
COUNT(column) counts only rows where the column is NOT NULL. NULL values are skipped.
❓ query_result
intermediate2:00remaining
COUNT(*) vs COUNT(column)
Consider a table Orders with 100 rows, where the column
ShippedDate has 20 NULL values. What will be the result of these two queries?1) SELECT COUNT(*) FROM Orders;
2) SELECT COUNT(ShippedDate) FROM Orders;
SQL
SELECT COUNT(*), COUNT(ShippedDate) FROM Orders;
Attempts:
2 left
💡 Hint
COUNT(*) counts all rows, COUNT(column) counts non-NULL values in that column.
✗ Incorrect
COUNT(*) counts all rows regardless of NULLs. COUNT(ShippedDate) counts only non-NULL ShippedDate values.
🧠 Conceptual
advanced2:00remaining
COUNT with DISTINCT
What does the query below return?
SELECT COUNT(DISTINCT CustomerID) FROM Sales;
SQL
SELECT COUNT(DISTINCT CustomerID) FROM Sales;
Attempts:
2 left
💡 Hint
DISTINCT counts unique values, COUNT ignores NULLs.
✗ Incorrect
COUNT(DISTINCT column) counts unique non-NULL values in that column.
📝 Syntax
advanced2:00remaining
Invalid use of COUNT function
Which of the following queries will cause a syntax error?
Attempts:
2 left
💡 Hint
COUNT requires an argument inside the parentheses.
✗ Incorrect
COUNT() with empty parentheses is invalid syntax and causes an error.
❓ optimization
expert2:00remaining
Optimizing COUNT queries on large tables
You have a very large table Logs with millions of rows. You want to count how many rows have
Status = 'Error'. Which query is generally the most efficient?Attempts:
2 left
💡 Hint
COUNT(*) counts rows and is optimized by most databases.
✗ Incorrect
COUNT(*) with a WHERE clause is optimized to count rows matching the condition efficiently.