×

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 INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

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

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

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 Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

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

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 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 Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.