×

How to use COUNT in SQL?

How to use COUNT in SQL

Introduction

  • COUNT( ) is an aggregate function in SQL.
  • This function counts the number of records in a table if the condition is not specified.
  • If the condition is specified, the count function returns the number of records satisfying the specified condition.

Variations of COUNT ( ) in SQL

  1. COUNT(*)

COUNT (*) is used to display the number of records present in a table.

The results of COUNT (*) function will contain NULL as well as duplicate entries also into consideration.

Syntax:

SELECT COUNT (*) FROM tablename;

Example:

First we will create a database with name “employeedb”. Then in that database we will create a table “employee” and insert records into the table. We will consider this table and database for all the subsequent examples.

We will find the number of records present in ‘employee’ table using COUNT () function.

 mysql> USE employeedb;
 Database changed
 mysql> SELECT *FROM employee;
 +--------+----------+------------+
 | Emp_ID | Emp_Name | Emp_Salary |
 +--------+----------+------------+
 |      1 | Nikita   |      30000 |
 |      2 | Riddhi   |      25000 |
 |      3 | Nayan    |      45000 |
 |      4 | Shruti   |      15000 |
 |      5 | Anurati  |      55000 |
 |      6 | NULL     |       NULL |
 |      7 | Anurati  |      55000 |
 +--------+----------+------------+
 7 rows in set (0.00 sec) 
How to use COUNT in SQL?
mysql> SELECT COUNT(*) FROM employee;

Output:

 +----------+
 | COUNT(*) |
 +----------+
 |        7 |
 +----------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 7 records present in employee table.

  • COUNT (1)

COUNT (1) is also used to display the number of records present in a table. The results of COUNT (1) function will contain NULL as well as duplicate entries also into consideration. COUNT (1) function works same as COUNT (*). Even the results of COUNT (1) and COUNT (*) are also same.

Syntax:

SELECT COUNT (1) FROM tablename;

Example 1: We will display the number of records present in employee table using COUNT (1).

mysql> SELECT COUNT(1) FROM employee;

Output:

 +----------+
 | COUNT(1) |
 +----------+
 |        7 |
 +----------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 7 records present in a table.

Example 2:

Let us see what happens when we pass 14 as a parameter to COUNT() function.

mysql> SELECT COUNT(14) FROM employee;

Output:

 +-----------+
 | COUNT(14) |
 +-----------+
 |         7 |
 +-----------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 7 records present in a table. So, 7 is displayed as an output even though we have passed 14 as a parameter to COUNT() function. Passing an integer to a COUNT() function does not mean to count those number of rows in table. It simply means 14 will be assigned to each and every row present in  a table and then that rows will be summed up to give a total and displayed as an output.

Example 3:

Let us see what happens when we pass -14 as a parameter to COUNT() function.

mysql> SELECT COUNT(-14) FROM employee;

Output:

 +------------+
 | COUNT(-14) |
 +------------+
 |          7 |
 +------------+
 1 row in set (0.07 sec) 
How to use COUNT in SQL?

There are 7 records present in a table. So, 7 is displayed as an output even though we have passed -14 as a parameter to COUNT() function. It simply means -14 will be assigned to each and every row present in a table and then that rows will be summed up to give a total and displayed as an output.

  • COUNT(ColumnName)

COUNT(ColumnName) is used to find the number of records which contain values for the specified column. While using COUNT() function with columnname as a parameter, the records which contain NULL values for that record will be ignored.

Syntax:

SELECT COUNT(ColumnName) FROM tablename;

Example 1:

We will display the number of records which exists for Emp_ID.

mysql> SELECT COUNT(Emp_ID) FROM employee;

Output:

 +---------------+
 | COUNT(Emp_ID) |
 +---------------+
 |             7 |
 +---------------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 7 records which contain unique Emp_ID. Therefore, 7 is displayed as an output.

Example 2:

We will display the number of records which exists for Emp_Name.

mysql> SELECT COUNT(Emp_Name) FROM employee;

Output:

 +-----------------+
 | COUNT(Emp_Name) |
 +-----------------+
 |               6 |
 +-----------------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 7 records in employee table among which, one record contain NULL values for Emp_Name. So, that particular record is ignored and 6 is displayed as an output.

Example 3:

We will display the number of records which exists for Emp_Salary.

mysql> SELECT COUNT(Emp_Salary) FROM employee;

Output:

 +-------------------+
 | COUNT(Emp_Salary) |
 +-------------------+
 |                 6 |
 +-------------------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 7 records in employee table among which one record contain NULL values for Emp_Salary. So, that particular record is ignored and 6 is displayed as an output.

  • COUNT(DISTINCT ColumnnName)

COUNT() function with DISTINCT ColumnName as its parameter is used to display the number of records which contains unique values for a specific column. Records which contain duplicate and NULL values will not be counted.

Syntax:

SELECT COUNT(DISTINCT ColumnName) FROM tablename;

Example 1:

We will display the number of records which contain unique values for Emp_ID.

mysql> SELECT COUNT( DISTINCT Emp_ID) FROM employee;

Output:

 +-------------------------+
 | COUNT( DISTINCT Emp_ID) |
 +-------------------------+
 |                       7 |
 +-------------------------+
 1 row in set (0.05 sec) 
How to use COUNT in SQL?

There are 7 records which contain unique values for Emp_ID.

Example 2:

We will display the number of records which contain unique values for Emp_Name.

mysql> SELECT COUNT( DISTINCT Emp_Name) FROM employee;

Output:

 +---------------------------+
 | COUNT( DISTINCT Emp_Name) |
 +---------------------------+
 |                         5 |
 +---------------------------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 5 records which contain unique values for Emp_Name. NULL and duplicate values in Emp_Name will not be considered by the DISTINCT keyword.

Example 3:

We will display the number of records which contain unique values for Emp_Salary.

mysql> SELECT COUNT( DISTINCT Emp_Salary) FROM employee;

Output:

 +-----------------------------+
 | COUNT( DISTINCT Emp_Salary) |
 +-----------------------------+
 |                           5 |
 +-----------------------------+
 1 row in set (0.00 sec) 
How to use COUNT in SQL?

There are 5 records which contain unique values for Emp_Salary. NULL and duplicate values in Emp_Salary will not be considered by the DISTINCT keyword.


Related Topics

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.