Skip to main content
Documents
Clauses StatementsKeywordsExpressionsFunctionsIndexesPerformance Optimization

MySQL COUNT() Function

The `COUNT()` function in MySQL is used to return the number of rows that match a specified criterion. It is often employed in data analysis to determine the size of datasets or subsets within a database.

Usage

The `COUNT()` function is used to count the number of rows in a table or count the number of distinct values in a column. It is typically used in `SELECT` statements, often alongside `GROUP BY` to aggregate data.

SELECT COUNT(column_name)
FROM table_name
[WHERE condition];

In this syntax, `COUNT(column_name)` counts the non-NULL values in `column_name`. If you use `COUNT(*)`, it counts all rows, including those with NULLs. To count unique non-NULL values, you can use `COUNT(DISTINCT column_name)`.

Examples

1. Basic Row Count

SELECT COUNT(*)
FROM orders;

This example counts all the rows in the `orders` table, providing a total count of orders.

2. Counting Non-NULL Values

SELECT COUNT(customer_id)
FROM orders;

Here, the `COUNT(customer_id)` function counts only the non-NULL customer IDs in the `orders` table, which can be useful if some orders do not have associated customer IDs.

3. Counting with a Condition

SELECT COUNT(order_id)
FROM orders
WHERE status = 'Completed';

This query counts only the `order_id` values where the order status is 'Completed', giving insight into the number of completed orders.

4. Counting Unique Values

SELECT COUNT(DISTINCT customer_id)
FROM orders;

This example counts the number of unique non-NULL customer IDs in the `orders` table, providing a count of distinct customers who have placed orders.

Tips and Best Practices

  • Use COUNT(*) for total rows. Use `COUNT(*)` to get the total number of rows, as it is optimized for counting all rows without checking for NULLs or specific column values. It is generally faster than counting a specific column due to its simplicity.
  • Specify columns to avoid NULLs. Use `COUNT(column_name)` to exclude rows where the specified column has NULL values.
  • Combine with GROUP BY. Use `COUNT()` with `GROUP BY` to get counts of distinct groups within your data. When used with `GROUP BY`, `COUNT(*)` provides the count of rows per group.
  • Optimize with indexes. Ensure columns used in `COUNT()` queries are indexed for improved performance, especially on large datasets. Proper indexing can significantly enhance query performance and reduce execution time.

SQL Upskilling for Beginners

Gain the SQL skills to interact with and query your data.
Start Learning for Free