Introduction to MySQL count()
The MySQL COUNT() function provides a number of records in the result set from a table when an SQL SELECT statement is executed. This function does not count the NULL values. The count function gives a BIGINT value. This aggregate function returns all rows or only rows which are matched to specified conditions and if there is no row that matches then it returns 0. Here, Aggregate function is a function that calculates some values and returns only a single value. We can use COUNT() in many ways but necessary is to understand how it works. We can receive different result values according to how we use it.
The basic syntax for count function is:
COUNT( [DISTINCT] expression )
Explanation: The function has three forms explained as follows:
- COUNT(*) function: It returns all the number of rows in a table returned by the SELECT query including non-NULL, NULL, and duplicate rows from the table.
- COUNT(expression) function: It provides the result of the expression counting the number of rows without NULL values.
- COUNT(DISTINCT expression): It results in the number of rows containing non- NULL distinct value in the result set returned by the SELECT statement.
How does the COUNT() function work in MySQL?
The Count is an aggregate function that returns a single concise result working on the entire set of rows in a table.
There are different tables stored in the Database with various sizes and numbers of rows. We can find millions of records or more in tables like transaction tables. Therefore, monitoring the progress of tables and getting the count of records becomes a very important factor in the process of development and operations.
Examples to Implement MySQL Count()
Let us say we have a table named Customers as a sample:
If we use the MySQL COUNT(expression) function to get all records from the table using an expression value which does not contain a NULL value. The SQL query is:
SELECT COUNT(City) FROM Customers;
For the same table let us useMySQLCOUNT(*) function. Suppose we have executed the following statement:
SELECT COUNT(*) FROM Customers;
Explanation: This count function will return the rows count from the result set which may contain or not contain NULL values.
Now we will discuss onMySQLCOUNT(DISTINCT expression):
SELECT COUNT(DISTINCT City) FROM Customers;
Explanation: Then, this function the DISTINCT keyword will show the count for the records that are unique, not duplicate or repeated, and the values that are not NULL.
Now, we can also use Count function like this MySQL COUNT() with GROUP BY. With the count() we can use GROUP BY SQL clause so that it provides the number of records inside a group:
SELECT City, COUNT(*) FROM Customers GROUP BY City;
Explanation: In the above example, count() with GROUP BY keyword groups all distinct cities and returns the count of each one.
We can also use MySQL Count() with The HAVINGClause in MySQL statement. In the above example we can add Having clause to filter the result from the above query further:
SELECT City, COUNT(*) FROM Customers GROUP BY City HAVING COUNT(*) >1;
Explanation: Here, we have added count() with the HAVING clause which results in the count of records from the table Customers GROUP BY City that have count greater than 1. The NULL value field is also counted.
The Count() function can be combined with the Flow Control functions You can associate Count() function with flow control functions to achieve better functionality.
For example, flow control function IF() can be used with the expression that will be used for the Count() function in SQL Statement. This will be pretty supportive to offer a quick analysis of the data inside a database. Let us consider the data from a table named Products with three fields ProductID, ProductName, Price, etc:
Here, is a field called Price in the table and every product has different prices recorded in that row. So, we can use this Price field to do the following query execution with COUNT() and IF() function in the SQL statement to return the result count:
COUNT(IF(Price <=10,1,NULL)) 'Low',
COUNT(IF(Price BETWEEN 10 AND 30,1,NULL)) 'Medium',
COUNT(IF(Price > 30,1,NULL)) 'High'
Explanation: From the above statement, we can see that on the basis of Price in the Products table, we have divided them into three groups named as Low, Medium, and High. Here we have used the IF() function in combination to count() function to compare the different prices of the products to a given condition and when the condition is matched, then it returns the result as above, where the count for 3 groups; low, medium, high are divided that fulfills the expression demand. If there is no match to a condition then, it will return a NULL value that means 0.
We can even use the UNION operator with the COUNT function to obtain MySQL row count of two or more tables. Using UNION, we can unite the result sets gained from every individual SELECT statement and apply count to get the number of rows of multiple tables in a database.
For example, we have executed a single SQL query below to return the row count of two tables Customers and Products from our sample database:
SELECT 'Customers' tablename, COUNT(*) rows FROM Customers
SELECT 'Products' tablename, COUNT(*) rows FROM Products;
SELECT COUNT(*) FROM Customers
SELECT COUNT(*) FROM Products;
We can use COUNT() in combination with the WHERE clause in the SELECT statement query if we want to count some rows of the table. Here, the number of expressions defined in the count function will be returned from the table based on particular criteria of WHERE clause on the SELECT query. Let us take the previous Products table:
SELECT COUNT(ProductName) FROM Products WHERE SupplierID = 1;
MySQL COUNT Function can be said as the simplest function and very beneficial for counting the number of rows in a table that satisfies certain conditions with SELECT statement execution. In this article, we have also learned to return a non NULL count of rows from one or multiple tables in the MySQL Database using various MySQL clauses and operators with syntax and examples.
This is a guide to MySQL count(). Here we discuss an introduction to MySQL count() with appropriate Syntax, working, and respective examples for better understanding. You can also go through our other related articles to learn more –