×

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: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 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 Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL DATE_FORMAT() function

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

3 minutes read.

MySQL INSERT() Function

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

2 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 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 database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL SEC_TO_TIME() function

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

2 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 Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL ROUND() function

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

3 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.