×

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the count of the data set. It works on numerical data type values. If the table displays zero value, the row does not exist in the table.

The count function can use several arguments inside of the bracket. The count function represents in three forms whose description shows below:

  • COUNT(*): It returns the count of all duplicate, null, and not null rows of the table.
  • COUNT(function expression): It displays all duplicate and not null rows of the table. You cannot show null rows of the table.
  • COUNT(DISTINCT expression): This argument shows unique and not null rows of the table. You cannot show duplicate and null rows of the table.

Syntax

The COUNT () function syntax shows below.

COUNT(function_expression)

The COUNT () function with table syntax displays below.

SELECT COUNT(function_expression) FROM Table_Name;

COUNT () function with table data syntax displays below.

SELECT COUNT(*) FROM Table_Name;

COUNT () function with distinct clause syntax displays below.

SELECT COUNT(DISTINCT function_expression) FROM Table_Name;

COUNT () function with condition syntax shows below.

SELECT COUNT(function_expression) FROM Table_Name WHERE condition;

Description

  • The COUNT() function returns a number of the rows of the table.
  • The "function_expression" represents the rows for function operation.
  • The "WHERE" clause applies to filter the records of the table.
  • The "*" symbol shows the entire table data and values for the COUNT function.
  • The DISTINCT clause removes duplicate data of the given columns.

Prerequisite for COUNT function

  • Create a new table using the below statements.

mysql> create table arts (  

    product_id int NOT NULL,

    product_name varchar(100) NOT NULL,    

    available_quantity int,  

    required_quantity int,  

    Date date

    );  

  • Insert the data in the table.

mysql> insert into arts(product_id, product_name, available_quantity,  

required_quantity, Date) VALUES      

(1, 'paper', 78, 45, '2021-08-11'),  

(2, 'sketch book', 100, 65, '2021-09-01'),  

(3, 'pencil', 78, 45, '2021-10-14'),  

(4, 'brush pen', 85, 25, '2021-12-21'),  

(5, 'eraser', 95, 56, '2021-07-23'),  

(6, 'sketch pen', 99, 66, '2021-10-13');

  • You can refer to the table structure and information using the output query.
mysql> SELECT * FROM arts;

The table shows the five columns and six rows.

Examples of the COUNT function

1) Example: This example shows the COUNT function with expression and its output.

mysql> SELECT COUNT(required_quantity) AS products FROM arts;

 Output

MySQL COUNT Function

The count function displays six rows of the product column.  

2) Example: This example displays the COUNT function with the * argument and its output. The "*" symbol counts entire columns and rows of the table.

mysql> SELECT COUNT(*) AS products FROM arts;

 Output

MySQL COUNT Function

The output image shows all available rows of the table. The query can contain null or not null values. The "arts" table contains six columns of the entire columns.

3) Example: This example uses the DISTINCT clause in the COUNT function. This DISTINCT clause applies to the "required_quantity" column of the "arts" table. The DISTINCT function removes duplicate values of the given column.

mysql> SELECT COUNT(DISTINCT required_quantity) AS products FROM arts;

 Output

MySQL COUNT Function

The output image shows the table has five unique rows. The distinct clauses remove the "45" data of the required_quantity column.

4) Example: The COUNT functions with multiple columns example.

Execute the below query with the product_id, available_quantity,  required_quantity, and Date columns in the count function.

mysql> SELECT COUNT(product_id) AS id,
COUNT(available_quantity) AS availability,
COUNT(required_quantity) AS necessity,
 COUNT(Date) AS received 
FROM arts;

Output

MySQL COUNT Function

5) Example: The COUNT functions using the "WHERE" clause example.

Execute the COUNT function with multiple columns of the table. This function uses the WHERE clause for the conditional operation.

mysql> SELECT COUNT(product_id) AS id,
COUNT(available_quantity) AS availability,
COUNT(required_quantity) AS necessity,
 COUNT(Date) AS received 
FROM arts WHERE available_quantity < 90;

Output

MySQL COUNT Function

MySQL count function exhibits three rows of the "arts" table. These rows represent less than 90 values of the available_quantity column.

6) Example: The COUNT functions with "WHERE" and "DISTINCT" clauses example shows below. The "DISTINCT" clause is used on all columns and the "WHERE" clause on a single column.

mysql> SELECT COUNT(DISTINCT product_id) AS id,
COUNT(DISTINCT available_quantity) AS availability,
COUNT(DISTINCT required_quantity) AS necessity,
 COUNT(DISTINCT Date) AS received 
FROM arts WHERE available_quantity < 90;

Output

MySQL COUNT Function

The count function shows the output table having four columns and one clause. The "WHERE" clause uses the "less than 90 value" of the "available_quantity" column. The first and last columns show three counts of the data set. The second and third rows show two counts of the data set because of the DISTINCT clause.

7) Example: The COUNT functions with the "GROUP BY" clause example shows below. The "GROUP BY" uses the "available_quantity" column of the "arts" table. You can see the numerical and date data type column in the count function query.

mysql> SELECT COUNT(product_id) AS id,
COUNT(available_quantity) AS availability,
COUNT(required_quantity) AS necessity,
 COUNT(Date) AS received 
FROM arts GROUP BY available_quantity;

Output

MySQL COUNT Function

The output image represents the table values using the count function. The first row shows similar values of the available_quantity column.  The other rows show one value that represents the unique row of the column.

8) Example: The COUNT functions with the "GROUP BY" and "HAVING" clause example displays below. The "GROUP BY" uses the "available_quantity" column, and the HAVING clause applies the "greater than" condition.

mysql> SELECT COUNT(product_id) AS id,
COUNT(available_quantity) AS availability,
COUNT(required_quantity) AS necessity,
 COUNT(Date) AS received 
FROM arts GROUP BY available_quantity HAVING available_quantity > 80;

Output

MySQL COUNT Function

MySQL count function shows four rows of the table as output. This table displays Boolean values using the "HAVING" condition.

9) Example: The COUNT functions using the "limit" clauses example shows below.

MySQL count function comes with the "limit" clause. You can use the limit clause with the other MySQL clauses and conditions.

mysql> SELECT COUNT(product_id) AS id,
COUNT(available_quantity) AS availability,
COUNT(required_quantity) AS necessity,
 COUNT(Date) AS received 
FROM arts GROUP BY available_quantity HAVING available_quantity > 80 limit 2;

Output

MySQL COUNT Function

The count function executes two rows because of the limit clause and its two (2) values. The output returns Boolean values as per the condition.


Related Topics

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL CONCAT() function

In this context, we will learn how we can use the MySQL CONCAT() function with proper syntax and good examples. Introduction of MySQL CONCAT() function By using the MySQL CONCAT function, we...

3 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL DAY() function

In this context, we will learn how we can use the MySQL DAY() function with proper syntax and good examples. Introduction of MySQL DAY() function This function in MySQL is used to...

2 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL RAND() function

In this context, we will learn how we can use the MySQL RAND() function with proper syntax and good examples. Introduction of MySQL RAND() function Basically, in MySQL the RAND() function is...

3 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.