Bird
Raised Fist0
SQLquery~20 mins

COUNT function behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the SQL function COUNT(*) do when used in a query?
easy
A. Counts only rows with NULL values
B. Counts only rows where all columns are NOT NULL
C. Counts all rows in the table, including those with NULL values
D. Counts only distinct values in a column

Solution

  1. Step 1: Understand COUNT(*) behavior

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

    Unlike COUNT(column), which skips NULLs, COUNT(*) includes all rows.
  3. Final Answer:

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

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

Solution

  1. Step 1: Identify how to count non-NULL values

    COUNT(column) counts only non-NULL values in that column.
  2. Step 2: Check syntax correctness

    SELECT COUNT(age) FROM persons; correctly counts non-NULL age values.
  3. Final Answer:

    SELECT COUNT(age) FROM persons; -> Option B
  4. Quick Check:

    COUNT(column) counts non-NULL values [OK]
Hint: Use COUNT(column) to count non-NULL values [OK]
Common Mistakes:
  • Using COUNT(*) to count non-NULL values
  • Using WHERE clause unnecessarily
  • Using COUNT with boolean expressions
3. Given the table employees with the column department containing values: ['HR', 'IT', NULL, 'IT', 'HR', 'Finance', NULL], what is the result of the query SELECT COUNT(DISTINCT department) FROM employees;?
medium
A. 3
B. 5
C. 7
D. 4

Solution

  1. Step 1: Identify distinct non-NULL values in department

    The distinct non-NULL values are 'HR', 'IT', and 'Finance'. That's 3 unique values.
  2. Step 2: Understand COUNT(DISTINCT) behavior

    COUNT(DISTINCT column) counts unique non-NULL values only, so NULLs are excluded.
  3. Step 3: Count distinct values

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

    3 -> Option A
  5. Quick Check:

    COUNT(DISTINCT) excludes NULLs [OK]
Hint: COUNT(DISTINCT) counts unique non-NULL values only [OK]
Common Mistakes:
  • Including NULL as a distinct value
  • Counting total rows instead of distinct
  • Confusing COUNT(*) with COUNT(DISTINCT)
4. Consider the query: SELECT COUNT(employee_id) FROM staff; but it returns 0 even though the table has rows. What is the most likely reason?
medium
A. The column employee_id contains only NULL values
B. The table staff is empty
C. COUNT(employee_id) counts all rows including NULLs
D. Syntax error in the query

Solution

  1. Step 1: Understand COUNT(column) behavior

    COUNT(column) counts only non-NULL values in that column.
  2. Step 2: Analyze why count is zero

    If all employee_id values are NULL, COUNT returns 0 even if rows exist.
  3. Final Answer:

    The column employee_id contains only NULL values -> Option A
  4. Quick Check:

    COUNT(column) excludes NULLs [OK]
Hint: COUNT(column) returns 0 if all values are NULL [OK]
Common Mistakes:
  • Assuming COUNT(column) counts all rows
  • Thinking table is empty without checking data
  • Assuming syntax error without checking query
5. You want to find how many unique customers placed orders, but some orders have NULL customer IDs. Which query correctly counts unique customers excluding NULLs?
hard
A. SELECT COUNT(customer_id) FROM orders;
B. SELECT COUNT(*) FROM orders WHERE customer_id IS NOT NULL;
C. SELECT COUNT(DISTINCT *) FROM orders;
D. SELECT COUNT(DISTINCT customer_id) FROM orders;

Solution

  1. Step 1: Understand requirement for unique customers excluding NULLs

    We need to count distinct customer IDs ignoring NULLs.
  2. Step 2: Analyze options

    SELECT COUNT(DISTINCT customer_id) FROM orders; uses COUNT(DISTINCT customer_id), which counts unique non-NULL values correctly.
  3. Step 3: Eliminate incorrect options

    SELECT COUNT(customer_id) FROM orders; counts all non-NULL customer IDs including duplicates. SELECT COUNT(*) FROM orders WHERE customer_id IS NOT NULL; counts rows with non-NULL customer IDs but not distinct. SELECT COUNT(DISTINCT *) FROM orders; is invalid syntax.
  4. Final Answer:

    SELECT COUNT(DISTINCT customer_id) FROM orders; -> Option D
  5. Quick Check:

    COUNT(DISTINCT column) counts unique non-NULL values [OK]
Hint: Use COUNT(DISTINCT column) for unique non-NULL counts [OK]
Common Mistakes:
  • Using COUNT(column) to count unique values
  • Using COUNT(*) with WHERE instead of DISTINCT
  • Using invalid syntax like COUNT(DISTINCT *)