0
0
SQLquery~20 mins

COUNT function behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
COUNT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
AReturns an error because COUNT cannot be used on nullable columns
BCounts all rows including those with NULL ManagerID
CReturns the total number of NULL ManagerID values
DCounts only rows where ManagerID is NOT NULL
Attempts:
2 left
💡 Hint
Remember that COUNT(column) ignores NULL values in that column.
query_result
intermediate
2: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;
A1) 100, 2) 100
B1) 80, 2) 100
C1) 100, 2) 80
D1) 80, 2) 80
Attempts:
2 left
💡 Hint
COUNT(*) counts all rows, COUNT(column) counts non-NULL values in that column.
🧠 Conceptual
advanced
2:00remaining
COUNT with DISTINCT
What does the query below return?
SELECT COUNT(DISTINCT CustomerID) FROM Sales;
SQL
SELECT COUNT(DISTINCT CustomerID) FROM Sales;
AThe number of unique CustomerID values, excluding NULLs
BThe total number of rows in Sales including duplicates
CThe number of rows where CustomerID is NULL
DThe total number of unique rows in Sales
Attempts:
2 left
💡 Hint
DISTINCT counts unique values, COUNT ignores NULLs.
📝 Syntax
advanced
2:00remaining
Invalid use of COUNT function
Which of the following queries will cause a syntax error?
ASELECT COUNT(DISTINCT CategoryID) FROM Products;
BSELECT COUNT() FROM Products;
CSELECT COUNT(ProductID) FROM Products;
DSELECT COUNT(*) FROM Products;
Attempts:
2 left
💡 Hint
COUNT requires an argument inside the parentheses.
optimization
expert
2: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?
ASELECT COUNT(*) FROM Logs WHERE Status = 'Error';
BSELECT COUNT(Status) FROM Logs WHERE Status = 'Error';
CSELECT COUNT(1) FROM Logs WHERE Status = 'Error';
DSELECT COUNT(DISTINCT Status) FROM Logs WHERE Status = 'Error';
Attempts:
2 left
💡 Hint
COUNT(*) counts rows and is optimized by most databases.