×

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

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

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.

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

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.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 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 Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

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

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

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.