Middle
What is the difference between COUNT(*) and COUNT(1) — which one is better to use?
sobes.tech AI
Answer from AI
In SQL queries, COUNT(*) and COUNT(1) are often used to count the number of rows, but there is practically no difference between them in terms of result and performance in most modern DBMS.
COUNT(*)counts all rows, including those where all fields are NULL.COUNT(1)counts the number of rows by substituting the constant 1 into the expression, which is always not NULL, so the result matchesCOUNT(*).
Internally, database optimizers usually handle both options with the same efficiency. Therefore, it is better to use COUNT(*) — it is the standard and more readable way to count all rows.
Example:
SELECT COUNT(*) FROM employees;
SELECT COUNT(1) FROM employees;
Both will return the same number — the count of rows in the employees table.