0
0
MySQLquery~20 mins

COUNT function in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
COUNT function 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 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);
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
COUNT(column) counts only non-NULL values in that column.
query_result
intermediate
2: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);
A4, 2
B2, 4
C4, 4
D2, 2
Attempts:
2 left
💡 Hint
COUNT(*) counts all rows, COUNT(column) counts only non-NULL values in that column.
📝 Syntax
advanced
2: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 Sales with product_id and quantity, you want to find products sold more than 10 times.
Which query is valid?
ASELECT product_id FROM Sales WHERE SUM(quantity) > 10;
BSELECT product_id FROM Sales GROUP BY product_id WHERE COUNT(quantity) > 10;
CSELECT product_id FROM Sales GROUP BY product_id HAVING COUNT(quantity) > 10;
DSELECT product_id FROM Sales WHERE COUNT(quantity) > 10;
Attempts:
2 left
💡 Hint
Conditions on aggregate functions must use HAVING, not WHERE.
optimization
advanced
2: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?
ASELECT COUNT(visit_date) FROM Visits WHERE visit_date = CURDATE();
BSELECT COUNT(DISTINCT visit_date) FROM Visits WHERE visit_date = CURDATE();
CSELECT COUNT(*) FROM Visits;
DSELECT COUNT(*) FROM Visits WHERE visit_date = CURDATE();
Attempts:
2 left
💡 Hint
COUNT(*) is optimized to count rows quickly. Filtering by date reduces scanned rows.
🧠 Conceptual
expert
2:00remaining
COUNT with JOIN and NULLs
Given two tables:
  • 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?
ASELECT Authors.name, COUNT(Books.id) FROM Authors JOIN Books ON Authors.id = Books.author_id GROUP BY Authors.name;
BSELECT Authors.name, COUNT(Books.id) FROM Authors LEFT JOIN Books ON Authors.id = Books.author_id GROUP BY Authors.name;
CSELECT Authors.name, COUNT(*) FROM Authors LEFT JOIN Books ON Authors.id = Books.author_id GROUP BY Authors.name;
DSELECT Authors.name, COUNT(Books.author_id) FROM Authors RIGHT JOIN Books ON Authors.id = Books.author_id GROUP BY Authors.name;
Attempts:
2 left
💡 Hint
Use LEFT JOIN to include authors without books. COUNT(column) ignores NULLs.