Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between count(*) and count(columnName)

Tags:

mysql

count

I would like to know what are the differences between using:

SELECT email, COUNT( email ) AS total
FROM `newsletter`
GROUP BY email having total>1

or

SELECT count(*) as total, email 
FROM 'newsletter' 
GROUP BY email having total > 1

Both give same results but what else is counting count(*) than the emails?

like image 725
Toni Michel Caubet Avatar asked Jan 10 '12 11:01

Toni Michel Caubet


People also ask

What is the difference between count (*) and count expression?

The difference is simple: COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values. Note that when you include a literal such as a number or a string in a query, this literal is "appended" or attached to every row that is produced by the FROM clause.

What is the difference between count (*) and count in SQL give example?

COUNT(*) counts the rows in your table. COUNT(column) counts the entries in a column - ignoring null values.

What is the difference between count (*) and Count 2?

Both expressions produce the same result. count(<expr>) takes in account all non- null values of <expr> . 2 is a literal, non-null values, so all rows are taken into account, just like count(*) does.

Why count 1 is faster than count (*)?

With COUNT(1), there is a misconception that it counts records from the first column. What COUNT(1) really does is that it replaces all the records you get from query result with the value 1 and then counts the rows meaning it even replaces a NULL with 1 meaning it takes NULLs into consideration while counting.


1 Answers

There's at least one difference.

  • They may return different results if email can contain NULL.

For more information, see this article.

like image 151
Sergio Tulentsev Avatar answered Sep 20 '22 11:09

Sergio Tulentsev