4 Ways to Use COUNT and When to Use Each
The COUNT() function is used to count the number of rows.
- COUNT(*)
- COUNT(column)
- COUNT DISTINCT
- NULL
The COUNT() function is used to count the number of rows.
There are 4 ways we can use COUNT(): 1. Count the total number of rows in a specific table 2. Count the rows with non-null data in a specific column 3. Count the rows with unique (non-duplicate) values in a specific column 4. Count the rows for each group of data
Suppose we have a table named friends like this:
id| name
--|---------
1 | Osama
2 | Ahmed
3 | NULL
4 | Hussein
5 | Ahmed
6 | Osama1. Number of Rows in a Specific Table
Of course, we don't care about the number of columns when we want to count the total number of rows in the table in general.
If I ask you to count the rows in the friends table, you would write:
SELECT COUNT(*) AS num_friends
FROM friendsAnd the output will be:
num_friends
------------
6Indeed, the number of rows in the table is 6, because I wrote the asterisk (*) inside COUNT, which means I am telling it to count all the rows in the table across all columns. I will explain what across all columns means later.
2. Number of Rows with Non-Missing Data in a Specific Column
The first method we used is not ideal in all cases.
Because sometimes when we work on data and write a query to extract information we need, your data might not be clean or accurate, so it might not count properly.
For example, if you look at the friends table, you will find that in row 3, the name column is empty (NULL). This doesn't mean this friend has no name; it means I forgot to write their name. This happens in a lot of data we work with—it is not clean data.
In this case, we will need to write something a bit more accurate than that. So we write the name of the column whose non-missing values we want to count inside COUNT.
If I ask you to count the values that are not missing in the name column, you would write:
SELECT COUNT(name) AS num_friends
FROM friendsThe output will be:
num_friends
------------
5The row count came out to 5 because row 3 is empty in the name column.
3. Number of Rows with Non-Duplicate Data in a Specific Column
To get unique (non-duplicate) values, we use DISTINCT and write it before the column name inside COUNT. If you don't know what DISTINCT is, you can understand what it does with examples here: For Those Who Do Not Know DISTINCT
If I ask you to get the count of unique (non-duplicate) names, you would write:
SELECT COUNT(DISTINCT name) AS num_friends
FROM friendsThe output will be:
num_friends
------------
3The count came out to 3 because Ahmed and Osama are each repeated once in the name column, and because row 3 is also empty (NULL).
4. Number of Rows for Each Group of Data
As soon as you hear "group of data," you immediately know I am going to talk about GROUP BY. If you don't know what GROUP BY does, read this topic where I explained it in a simple way: For Those Who Do Not Know GROUP BY
If we want to know how many times each friend's name is repeated, we write:
SELECT name, COUNT(*) AS num_friends
FROM friends
GROUP BY nameThe output will be:
name | num_friends
-------|-----
Osama | 2
Ahmed | 2
NULL | 1
Hussein| 1Notice that it counted NULL as well. Of course, that's because we wrote the asterisk (*) inside COUNT. As for Osama and Ahmed, their count is 2 because they are repeated twice.
What if we don't want to count NULL? We write the same query with a modification you can already predict:
SELECT name, COUNT(name) AS num_friends
FROM friends
GROUP BY nameAnd with an expected result as well:
name | num_friends
-------|-----
Osama | 2
Ahmed | 2
Hussein| 1