×

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

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

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

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.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

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

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

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 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 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 Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

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

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

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.

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 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 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 Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

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