Middle
What is the difference between SELECT COUNT(*) and SELECT COUNT(column) FROM table?
sobes.tech AI
Answer from AI
The difference between SELECT COUNT(*) and SELECT COUNT(column) in SQL is in which rows are counted:
-
SELECT COUNT(*) FROM table— counts all rows in the table, including those where column values may be NULL. -
SELECT COUNT(column) FROM table— counts only the rows where the value in the specified column is not NULL.
Example:
| id | name |
|---|---|
| 1 | Alice |
| 2 | NULL |
| 3 | Bob |
SELECT COUNT(*) FROM tablewill return 3.SELECT COUNT(name) FROM tablewill return 2, as one record contains NULL in the name column.
Thus, COUNT(column) is useful when you want to count only non-empty values in a specific column.