Challenge - 5 Problems
COUNT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding COUNT(*) vs COUNT(column)
What is the main difference between COUNT(*) and COUNT(column_name) in SQL?
Attempts:
2 left
💡 Hint
Think about how NULL values affect counting in SQL.
✗ Incorrect
COUNT(*) counts every row in the table, including those with NULL values in any column. COUNT(column_name) counts only rows where the specified column has a non-NULL value.
❓ query_result
intermediate2:00remaining
Result of COUNT(*) vs COUNT(column) with NULLs
Given the table
Employees with 5 rows where the ManagerID column has 2 NULL values, what will the following query return?
SELECT COUNT(*) AS total_rows, COUNT(ManagerID) AS non_null_managers FROM Employees;
Attempts:
2 left
💡 Hint
COUNT(*) counts all rows; COUNT(column) skips NULLs.
✗ Incorrect
There are 5 rows total, so COUNT(*) returns 5. Since 2 ManagerID values are NULL, COUNT(ManagerID) returns 3.
📝 Syntax
advanced2:00remaining
Identify the syntax error in COUNT usage
Which of the following SQL queries will cause a syntax error?
Attempts:
2 left
💡 Hint
COUNT accepts only one argument or *.
✗ Incorrect
COUNT only accepts a single column or *. Using two columns inside COUNT causes a syntax error.
❓ optimization
advanced2:00remaining
Optimizing COUNT queries with NULL columns
You want to count all rows in a large table
Sales. Which query is generally faster and why?
Options:
A) SELECT COUNT(*) FROM Sales;
B) SELECT COUNT(SaleID) FROM Sales; -- SaleID is NOT NULL
C) SELECT COUNT(Discount) FROM Sales; -- Discount can be NULL
D) SELECT COUNT(DISTINCT CustomerID) FROM Sales;Attempts:
2 left
💡 Hint
COUNT(*) is optimized by most databases to count rows quickly.
✗ Incorrect
COUNT(*) is usually the fastest way to count rows because it does not need to check any column values. Counting a specific column requires checking for NULLs, which can be slower.
🔧 Debug
expert2:00remaining
Why does COUNT(column) return zero unexpectedly?
You run this query:
SELECT COUNT(Email) FROM Users;But it returns 0, even though the table has 10 rows. What is the most likely reason?
Attempts:
2 left
💡 Hint
Remember COUNT(column) ignores NULL values.
✗ Incorrect
COUNT(column) counts only rows where the column is NOT NULL. If all Email values are NULL, the count is zero even if rows exist.