# How to Include Zero in a COUNT() Aggregate

You must encounter many occasions in your daily life where you must count on things, and counting things is rather simple, right? However, what would happen if you wanted to count something that’s not even there? That’s when the exciting part starts.

When you are using the COUNT() method in databases, there are situations in which you want to confirm that you count everything, even zero. In this tutorial, we will learn how to tackle this issue.

## Understanding COUNT() Aggregate

Before getting into why to include zero, let’s first understand what the COUNT() function does. The COUNT() function in databases plays a very crucial role, it counts the rows that meet certain conditions in the table. For example, you can count all the rows that are present in a table, or you can also count the ones that have something specific in them.

COUNT(*) function counts all the rows present in a table, whereas COUNT(column_name) counts the number of non-null values present in a specific column.

## Why Count Zero?

So, the first question in your mind could be ‘Why would anyone want to count zero?’ Now, let’s take a scenario where you’re having a birthday party, and you want to count how many pizzas each of your friends ordered. You want to count them, even if anyone does not order pizza. That’s where zero counting comes into the picture. Even if parts of it are empty, it helps to get the whole picture.

## How to Count Zero?

Now you must be wondering, how to actually count zero in database. Well, there are multiple ways to do that:

Now, let’s understand these one by one.

### 1. Using LEFT JOIN

In the LEFT JOIN, we merge the table from which we are counting with another table which contains all the possible value. After that, we just count. In this way, we can obtain a count that includes zero even in the case where the original table contains zero.

Example:

Imagine that you have two lists, in which one list contains all the names of your friends, and the other shows how many pizzas each friend has ordered. Using a LEFT JOIN is like placing up these lists side by side and matching the names. Even if someone’s name appears in the first list but not the second, they haven’t ordered any pizzas yet, they’ll still appear in the final count.

Below is the table named friends and pizzas_ordered:

Now let’s know which of your friends haven’t ordered a pizza yet, from the Friends and pizzas_ordered table:

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}```SELECT
pizza_ordered.friend_id,
COUNT(pizzas_ordered.pizzas_count) AS pizzas_ordered
FROM
friends
LEFT JOIN
pizzas_ordered
ON friends.friend_id = pizzas_ordered.friend_id
GROUP BY
friends.friend_id, friends.name;```Code language: SQL (Structured Query Language) (sql)```

Now we know that Amit hasn’t ordered any pizza yet as his name was not present at the second table and using LEFT JOIN we are able to figure out easily and count zero.

### 2. Using COALESCE

In this approach, we first create a mini-list which contains all possible values that we want to count. After that, we perform a COALESCE() operation on the original table, which refers to the mini list. This will ensure that the count includes zero even if there are no matching values in the original table. We have used it alongside the LEFT JOIN approach, let’s see it individually.

It is like saying “Hey, count how many pizzas each of my friends have ordered, and if they haven’t ordered any, just write zero” to the database.

Example:

Now, imagine a scenario where you have a record of messages sent by each user in a messaging system, and you have two tables: users and messages.

Now, you want to ensure that users who haven’t sent any messages are included in the result with a count of 0. Here’s how you can do this using COALESCE:

``````SELECT u.username,
COALESCE(COUNT(m.message_id), 0) AS messages_sent
FROM users u
LEFT JOIN message m ON u.user_id = m.sender_id