Last chance! 50% off DataCamp Premium
Sale ends in
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.
The `COUNT()` function is used to count the number of rows in a table or count the number of distinct values in a column, providing insights into data distribution and size.
sql
SELECT COUNT(column_name)
FROM table_name
[WHERE condition];
In this syntax, `COUNT(column_name)` returns the number of non-NULL values in a specified column, while `COUNT(*)` counts all rows in the result set, including those with NULL values.
sql
SELECT COUNT(*)
FROM orders;
This example counts all rows in the `orders` table, providing the total number of orders.
sql
SELECT COUNT(email)
FROM customers;
This syntax counts the number of non-NULL `email` entries in the `customers` table, useful for identifying the number of valid email addresses.
sql
SELECT COUNT(DISTINCT product_id)
FROM sales;
The query counts distinct `product_id` entries in the `sales` table, useful for determining how many different products have been sold. Note that using `COUNT(DISTINCT column_name)` can be computationally expensive with large datasets.
sql
SELECT COUNT(*)
FROM employees;
Use this to quickly get the total number of employees.
sql
SELECT COUNT(phone)
FROM customers;
This example counts only customers with a phone number provided.
sql
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
This query provides the number of employees in each department.
sql
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
This example only includes departments with more than 10 employees.
Understanding these applications of the `COUNT()` function will ensure accurate and efficient data analysis in MySQL.