Home >Database >Mysql Tutorial >COUNT(column) vs. COUNT(*): What's the Difference in SQL?
What is the difference between COUNT(column) and COUNT(*) when querying SQL tables?
COUNT(column) counts the number of non-null values in the specified column. In contrast, COUNT(*) counts all rows in a table, regardless of whether the specified column contains null values.
Consider the following form:
id | name |
---|---|
1 | John Doe |
2 | Jane Doe |
3 | NULL |
4 | NULL |
COUNT(column) ignores NULL values, while COUNT(*) treats them as valid entries. This behavior can affect query results, especially when applying conditions to the WHERE clause.
Consider the following query:
<code class="language-sql">SELECT name, COUNT(*) FROM table GROUP BY name HAVING COUNT(*) > 2;</code>
The above is the detailed content of COUNT(column) vs. COUNT(*): What's the Difference in SQL?. For more information, please follow other related articles on the PHP Chinese website!