Bird
Raised Fist0
MySQLquery~20 mins

COUNT function in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the COUNT(*) function do in MySQL?
easy
A. Counts all rows in the table, including those with NULL values
B. Counts only rows where all columns are NOT NULL
C. Counts only rows with NULL values in any column
D. Counts the number of columns in the table

Solution

  1. Step 1: Understand COUNT(*) behavior

    The COUNT(*) function counts every row in the table regardless of NULL values.
  2. Step 2: Compare with other COUNT variants

    Unlike COUNT(column), which counts only non-NULL values in that column, COUNT(*) counts all rows.
  3. Final Answer:

    Counts all rows in the table, including those with NULL values -> Option A
  4. Quick Check:

    COUNT(*) counts all rows [OK]
Hint: COUNT(*) counts every row, NULL or not [OK]
Common Mistakes:
  • Thinking COUNT(*) ignores rows with NULL
  • Confusing COUNT(*) with COUNT(column)
  • Assuming COUNT(*) counts columns
2. Which of the following is the correct syntax to count non-NULL values in the column age from the table users?
easy
A. SELECT COUNT(*) FROM users;
B. SELECT COUNT(age) FROM users WHERE age != NULL;
C. SELECT COUNT(age) FROM users WHERE age = NULL;
D. SELECT COUNT(age) FROM users;

Solution

  1. Step 1: Identify correct COUNT syntax for non-NULL values

    COUNT(column) counts only non-NULL values in that column, so COUNT(age) counts non-NULL ages.
  2. Step 2: Check WHERE clause correctness

    SELECT COUNT(*) FROM users; uses COUNT(*) which counts all rows. Using WHERE age IS NOT NULL is valid but unnecessary with COUNT(age). Options B and C use incorrect NULL comparisons.
  3. Final Answer:

    SELECT COUNT(age) FROM users; -> Option D
  4. Quick Check:

    COUNT(column) counts non-NULL values [OK]
Hint: Use COUNT(column) to count non-NULL values directly [OK]
Common Mistakes:
  • Using WHERE age = NULL instead of IS NOT NULL
  • Using COUNT(*) to count non-NULL values
  • Using != NULL which is invalid in SQL
3. Given the table orders with 5 rows where the discount column has 3 NULL values and 2 non-NULL values, what will this query return?
SELECT COUNT(discount) FROM orders;
medium
A. 5
B. 3
C. 2
D. 0

Solution

  1. Step 1: Understand COUNT(column) counts non-NULL values

    The query counts only rows where discount is NOT NULL.
  2. Step 2: Apply to given data

    There are 2 non-NULL discount values, so the count is 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    COUNT(discount) = 2 [OK]
Hint: COUNT(column) ignores NULLs, counts only real values [OK]
Common Mistakes:
  • Counting total rows instead of non-NULL values
  • Confusing NULL count with non-NULL count
  • Assuming COUNT(column) counts NULLs
4. Identify the error in this query:
SELECT COUNT(name) FROM employees WHERE name = NULL;
medium
A. Using COUNT(name) instead of COUNT(*)
B. Incorrect NULL comparison with '=' instead of IS NULL
C. Missing GROUP BY clause
D. COUNT cannot be used with WHERE clause

Solution

  1. Step 1: Check WHERE clause NULL comparison

    In SQL, NULL cannot be compared with '='; use IS NULL instead.
  2. Step 2: Validate COUNT usage

    COUNT(name) is valid and WHERE clause is allowed; no GROUP BY needed here.
  3. Final Answer:

    Incorrect NULL comparison with '=' instead of IS NULL -> Option B
  4. Quick Check:

    Use IS NULL for NULL checks [OK]
Hint: Use IS NULL, not = NULL, to check for NULL values [OK]
Common Mistakes:
  • Using '=' to compare NULL values
  • Thinking COUNT can't be combined with WHERE
  • Adding unnecessary GROUP BY
5. You have a table sales with columns region and amount. You want to find how many sales records exist for each region, including regions with zero sales. Which query will correctly show all regions with their sales count, including zero counts?
hard
A. SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region;
B. SELECT region, COUNT(*) FROM sales GROUP BY region;
C. SELECT region, COUNT(amount) FROM sales GROUP BY region;
D. SELECT region, COUNT(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region;

Solution

  1. Step 1: Understand requirement to include regions with zero sales

    To include regions with zero sales, we must use a LEFT JOIN from all regions to sales.
  2. Step 2: Analyze query options

    SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region; uses LEFT JOIN from regions table to sales, counting sales per region including zero counts. Options A, B, D only count existing sales records, missing zero counts.
  3. Final Answer:

    SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region; -> Option A
  4. Quick Check:

    LEFT JOIN + COUNT(column) includes zero counts [OK]
Hint: Use LEFT JOIN with COUNT(column) to include zero counts [OK]
Common Mistakes:
  • Using only sales table, missing regions with zero sales
  • Using COUNT(*) which counts joined rows, not zero
  • Not using LEFT JOIN to include all regions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard