×

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement.

Count() function includes Duplicates values also. If you want to discard the duplicate value count, use the DISTINCT keyword with the Count() function.

Syntax

Here is the syntax of the Count function in Structured Query Language.

SELECT COUNT(*) FROM TABLE_NAME; 

And this syntax will return the total number of records present inside the table.

SELECT COUNT(COLUMN_NAME) FROM TABLE_NAME;

We have to mention the column name after the COUNT keyword and the table name in this syntax.

Example

We consider the existing Employees table with certain columns as an example of Count Functions in Structured Query Language.

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE112F
1002VAIBHAVSHARMA60000NOIDAORACLE19M
1003NIKHILVANI50000JAIPURFMW210M
2001PRACHISHARMA55500CHANDIGARHORACLE110F
2002BHAVESHJAIN65500PUNEFMW212M
2003RUCHIKAJAIN50000MUMBAITESTING49F
3001PRANOTISHENDE55500PUNEJAVA39F
3002ANUJAWHERE50500JAIPURFMW29F
3003DEEPAMJAUHARI58500MUMBAIJAVA312M
4001RAJESHGOUD60500MUMBAITESTING410M
  • Write a query to count the total records from the Employees table.
SELECT COUNT(*)  AS TOTAL EMPLOYEES FROM EMPLOYEES;

The above query shows the following result it means there is a total of 10 records in the Employees table.

SQL COUNT
  • Write a query to count the total department number from the Employees table.
SELECT COUNT(DEPARTMENT) AS TOATLDEPARTMENT FROM EMPLOYEES;

The above query shows the following result. It included duplicate department values also.

SQL COUNT

Suppose you want to count unique values from the department field, then you will use DISTINCT Keywords inside the Count() function with Column Name.

  • Write a query to count the unique department of employees from the Employees table.
SELECT COUNT(DISTINCT DEPARTMENT) AS DEPARTMENT FROM EMPLOYEES;

The above query shows the following result. It shows only 4 counts because there are only 4 unique departments in the Employees table.

SQL COUNT
  • Write a query to count unique cities from the employee's table.
SELECT COUNT(DISTINCT CITY) AS CITY FROM EMPLOYEES;

The above query shows the following result. It shows only 5 counts because there are only 5 unique cities in the Employees table.

SQL COUNT
  • Write a query to count the total number of those employees who belong to city Pune.
SELECT COUNT(*) FROM EMPLOYEES WHERE CITY = ‘PUNE’;

Above query shows the following result:

SQL COUNT

There are a total of 3 employees who are from Pune city.

  • Write a query to count the total number of employees whose first name starts with 'V' and salary is greater than 50000.
SELECT COUNT(*) FROM EMPLOYEES WHERE FIRST_NAME  LIKE ‘V%’ AND SALARY > 50000;

Above query shows the following result:

SQL COUNT

There are only 2 employees whose first name starts with V and salary is greater than 50000.

  • Write a query to count the total numbers of the department from the employee's table joined with the manager table.
SELECT COUNT(DEPARTMENT) AS DEPARTMENT FROM EMPLOYEES INNER JOIN MANAGER ON EMPLOYEES.MANAGERID = MANAGER.MANAGER;

Above query shows the following result:

SQL COUNT

The above query return counts a total number of departments whose manager id is present in the employee's table from the manager table.


Related Topics

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 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 Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

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

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

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

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 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 SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

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

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 SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

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