0
0
SQLquery~20 mins

COUNT(*) vs COUNT(column) difference in SQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
COUNT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding COUNT(*) vs COUNT(column)
What is the main difference between COUNT(*) and COUNT(column_name) in SQL?
ACOUNT(*) counts all rows including those with NULLs in any column, while COUNT(column_name) counts only rows where the specified column is NOT NULL.
BCOUNT(*) and COUNT(column_name) always return the same result regardless of NULL values.
CCOUNT(*) counts only rows where the specified column is NULL, while COUNT(column_name) counts rows where the column is NOT NULL.
DCOUNT(*) counts only rows where all columns are NOT NULL, while COUNT(column_name) counts all rows regardless of NULLs.
Attempts:
2 left
💡 Hint
Think about how NULL values affect counting in SQL.
query_result
intermediate
2: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;
Atotal_rows = 5, non_null_managers = 3
Btotal_rows = 3, non_null_managers = 5
Ctotal_rows = 5, non_null_managers = 5
Dtotal_rows = 3, non_null_managers = 3
Attempts:
2 left
💡 Hint
COUNT(*) counts all rows; COUNT(column) skips NULLs.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in COUNT usage
Which of the following SQL queries will cause a syntax error?
ASELECT COUNT(OrderID) FROM Orders;
BSELECT COUNT(OrderID, CustomerID) FROM Orders;
CSELECT COUNT(*) FROM Orders;
DSELECT COUNT(DISTINCT CustomerID) FROM Orders;
Attempts:
2 left
💡 Hint
COUNT accepts only one argument or *.
optimization
advanced
2: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;
ASELECT COUNT(Discount) FROM Sales; because it skips NULLs and is faster.
BSELECT COUNT(SaleID) FROM Sales; because SaleID is NOT NULL and can be optimized.
CSELECT COUNT(*) FROM Sales; because it counts all rows without checking column values.
DSELECT COUNT(DISTINCT CustomerID) FROM Sales; because distinct counts are faster.
Attempts:
2 left
💡 Hint
COUNT(*) is optimized by most databases to count rows quickly.
🔧 Debug
expert
2: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?
AThe table Users is empty.
BCOUNT(Email) counts only rows where Email is an empty string.
CCOUNT(Email) counts only distinct emails, and there are none.
DAll values in the Email column are NULL.
Attempts:
2 left
💡 Hint
Remember COUNT(column) ignores NULL values.