Last Updated : 23 Jul, 2025
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 MySQLSyntax:
COUNT(expression)
Parameters:
*
, 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.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
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.
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.
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.
COUNT()
function do in MySQL?
How to useThe
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.
COUNT()
with conditions in MySQL?
What doesTo use
COUNT()
with conditions, you can combine it with theIF
function inside the query. For example,SELECT COUNT(IF(condition, 1, NULL))
counts rows where the condition is true.
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4