COUNT() Function in MySQL
Last Updated :
11 Sep, 2024
The COUNT()
function in MySQL is a versatile aggregate function used to determine the number of rows or non-NULL values that match a specific condition in a query.
It can be applied to an entire table or a particular column and is widely used in database operations to analyze the volume of data in a result set.
In this article, We will learn about COUNT() Function in MySQL by understanding various examples in detail.
COUNT() Function in MySQL
- The COUNT() function in MySQL is used to count the total number of rows that meet a specific condition or criteria.
- It is a commonly used aggregate function in SQL queries to determine the number of records in a table or result set.
Features of COUNT() Function in MySQL
- This function counts the number of rows returned by the query, based on the specified condition.
- It comes under Numeric Functions.
- It accepts only one parameter namely expression.
- This function ignores NULL values and doesn’t count them.
Syntax:
COUNT(expression)
Parameters:
- expression: This can be a column name, a wildcard
*
, or an expression. When using *
, COUNT()
counts all rows, including those with NULL
values in any column. When using a specific column or expression, it counts only non-NULL
values in that column.
Examles of COUNT() Function in MySQL
For better understanding of COUNT() Function in MySQL we will use the table called sales which is shown below:
id |
product_name |
quantity |
1 |
Laptop |
10 |
2 |
Smartphone |
20 |
3 |
Tablet |
NULL |
4 |
Smartwatch |
15 |
5 |
Laptop |
10 |
Example 1: COUNT(*)
Function
Let’s Determine the total number of rows in the sales
table, regardless of whether any column contains NULL
values.
SELECT COUNT(*) AS TotalRows FROM sales;
Output:
Explanation: This query counts all rows in the sales
table, regardless of whether any of the columns have NULL
values. There are 5 rows in total
Example 2: COUNT(expression)
Function
Let’s Write a query to count the number of rows in the sales
table where the quantity
column is not NULL
.
SELECT COUNT(quantity) AS NonNullQuantities FROM sales;
Output:
Explanation: This query counts only the rows where the quantity
column is not NULL
. Out of the 5 rows, only 4 rows have non-NULL
values for quantity.
Example 3: COUNT(DISTINCT expression)
Function
Let’s Determine the number of unique product names in the sales
table by using the COUNT(DISTINCT product_name)
function.
SELECT COUNT(DISTINCT product_name) AS UniqueProducts FROM sales;
Output:
Explanation: This query counts the distinct product names in the product_name
column. There are 4 unique products (Laptop
, Smartphone
, Tablet
, Smartwatch
). Even though Laptop
appears twice, it is only counted once.
Conclusion
The COUNT()
function in MySQL is essential for counting rows or non-NULL values in a table, whether evaluating the total number of records or identifying distinct entries. Its ability to work with different expressions makes it a fundamental tool for data analysis in SQL.
What does the COUNT()
function do in MySQL?
The COUNT()
function in MySQL returns the number of rows that match a specific condition. It can count all rows or only non-NULL values in a column.
How to use COUNT()
with conditions in MySQL?
To use COUNT()
with conditions, you can combine it with the IF
function inside the query. For example, SELECT COUNT(IF(condition, 1, NULL))
counts rows where the condition is true.
What does COUNT(*)
do in SQL?
The COUNT(*)
function counts all rows in a table, including those with NULL values in any column. It provides the total number of rows without considering NULLs