×

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

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 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 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 Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 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.

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.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 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 Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.