Challenge - 5 Problems
COUNT function 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 columns
id, name, and manager_id where some manager_id values are NULL, what is the result of this query?SELECT COUNT(manager_id) FROM Employees;
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(50), manager_id INT); INSERT INTO Employees VALUES (1, 'Alice', NULL), (2, 'Bob', 1), (3, 'Charlie', NULL), (4, 'Diana', 2);
Attempts:
2 left
💡 Hint
COUNT(column) counts only non-NULL values in that column.
✗ Incorrect
COUNT(manager_id) counts only rows where manager_id is not NULL. There are 2 such rows: Bob (manager_id=1) and Diana (manager_id=2).
❓ query_result
intermediate2:00remaining
COUNT(*) vs COUNT(column)
Consider the table Orders with columns
order_id, customer_id, and shipped_date. Some shipped_date values are NULL. What is the output of this query?SELECT COUNT(*), COUNT(shipped_date) FROM Orders;
MySQL
CREATE TABLE Orders (order_id INT, customer_id INT, shipped_date DATE); INSERT INTO Orders VALUES (1, 101, '2024-01-01'), (2, 102, NULL), (3, 103, '2024-01-03'), (4, 104, NULL);
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(shipped_date) counts only the 2 rows where shipped_date is not NULL.
📝 Syntax
advanced2:00remaining
Invalid use of COUNT in WHERE clause
Which option shows the correct way to count rows with a condition on the count itself?
Given a table
Which query is valid?
Given a table
Sales with product_id and quantity, you want to find products sold more than 10 times.Which query is valid?
Attempts:
2 left
💡 Hint
Conditions on aggregate functions must use HAVING, not WHERE.
✗ Incorrect
COUNT is an aggregate function and cannot be used in WHERE. HAVING filters groups after aggregation.
❓ optimization
advanced2:00remaining
Optimizing COUNT with large tables
You have a large table
Visits with millions of rows. You want to count how many visits happened today. Which query is the most efficient?Attempts:
2 left
💡 Hint
COUNT(*) is optimized to count rows quickly. Filtering by date reduces scanned rows.
✗ Incorrect
Option D counts all rows matching today's date efficiently. COUNT(visit_date) is similar but COUNT(*) is preferred for counting rows. Option D counts all rows ignoring date. Option D counts distinct dates, not visits.
🧠 Conceptual
expert2:00remaining
COUNT with JOIN and NULLs
Given two tables:
You want to count how many books each author has, including authors with zero books.
Which query produces the correct result?
Authors(id, name)Books(id, author_id, title)
You want to count how many books each author has, including authors with zero books.
Which query produces the correct result?
Attempts:
2 left
💡 Hint
Use LEFT JOIN to include authors without books. COUNT(column) ignores NULLs.
✗ Incorrect
LEFT JOIN keeps all authors. COUNT(Books.id) counts books per author. INNER JOIN (option B) excludes authors with zero books. COUNT(*) counts all joined rows, including NULLs from Books in LEFT JOIN, which can miscount.