×

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL.

There may be a situation that arises when you want to fetch the unique values from the tables, but duplicate values are also present inside the table. In such case, you should use the SELECT DISTINCT query to eliminate duplicate values and fetch unique values from the table.

Syntax of SELECT DISTINCT statement:

SELECT DISTINCT columnname1, columnname2 FROM table_name;

Consider the existing employee's tables which have the following records:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4

Let's understand the SELECT DISTINCT with the help of an example.

Example 1: Write a query to fetch employee details whose employee salary is greater than 55000 without using the DISTINCT keyword.

SELECT * FROM EMPLOYEES WHERE SALARY > 55000;

In the above statement, we have fetched the employee details from the employee's table whose employee salary is greater than 55000, including the duplicate values.

The output for the following statement:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
3001PRANOTISHENDE55500PUNEJAVA3
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4
SQL SELECT DISTINCT

The above result shows we have fetched the employees details, including duplicate values.

Example 2: Write a query to display employee salary from employees table whose employee salary is greater than 55000 using DISTINCT keyword.

SELECT DISTINCT (SALARY) FROM EMPLOYEES WHERE SALARY > 55000;

In the above statement, we are retrieving the employee salary from the employee's table whose salary is greater than 55000, where we are eliminating the duplicate values. Distinct salary will display unique salary from the employee's table.

The output for the following statement:

SALARY
65000
60000
55500
65500
58500
60500
SQL SELECT DISTINCT

In the first example, we have fetched the employee details whose salary is greater than 55000. The query result includes duplicate values. But in the second example, we execute the same query to display employee salary whose salary is greater than 55000. The query displays the result eliminating the duplicate values because we used the DISTINCT keyword followed by the salary column.

Example 3: Write a query to display employee city and salary from employees table without using a distinct keyword.

SELECT SALARY, CITY FROM EMPLOYEES;

We have displayed the employee salary and city name from the employee's table in the above statement, including duplicate values.

The output for the following statement:

SALARYCITY
65500PUNE
60000NOIDA
50500JAIPUR
55500CHANDIGARH
65500PUNE
50000MUMBAI
55500PUNE
50500JAIPUR
58500MUMBAI
60500MUMBAI
54500NOIDA
60000DELHI
55500DELHI
SQL SELECT DISTINCT

Example 4: Write a query to display employee city and salary from employees table using distinct keywords.

SELECT DISTINCT SALARY, CITY FROM EMPLOYEES;

We have fetched the employee salary and city from the employee table in the above statement. But we have fetched a unique value that is not repeated in the entire table using the DISTINCT keyword.

The output for the following statement:

SALARYCITY
65500PUNE
60000NOIDA
50500JAIPUR
55500CHANDIGARH
50000MUMBAI
55500PUNE
58500MUMBAI
60500MUMBAI
54500NOIDA
60000DELHI
55500DELHI
SQL SELECT DISTINCT

Example 5: Write a query to display an employee last name and department from the employee's table where the department name start with 'O' without using the DISTINCT keyword;

SELECT LAST_NAME, DEPARTMENT FROM EMPLOYEES WHERE DEPARTMENT LIKE 'O%';

In the above statement, we fetched employee last names, departments from the employee's table of those employees whose department names start with 'O', including duplicate values.

The output for the following statement:

LAST_NAMEDEPARTMENT
MISHRAORACLE
SHARMAORACLE
SHARMAORACLE
AGARWALORACLE
SQL SELECT DISTINCT

Only four records are displayed of those employees whose department starts with 'O'.

Example 6: Write a query to display an employee last name and department from the employee's table where the department name start with 'O' using the DISTINCT keyword;

SELECT DISTINCT LAST_NAME, DEPARTMENT FROM EMPLOYEES WHERE DEPARTMENT LIKE 'O%';

We have used the same query in example 5, just added the DISTINCT keyword just before columns name that will display unique values from the employee's table whose department name starts with 'O'.

The output for the following statement:

LAST_NAMEDEPARTMENT
MISHRAORACLE
SHARMAORACLE
AGARWALORACLE
SQL SELECT DISTINCT

The same query we have executed in the above query, we added the DISTINCT keyword just before columns. The output we get differs from the above example because the result display has unique values.

Example 7: Write a query to count employee salary from employees table without using a distinct keyword.

SELECT COUNT (SALARY) AS TOTAL SALARY FROM EMPLOYEES;

In the above statement, we have counted the total number of salaries from the employee's table, including duplicate values.

The output for the following statement:

TOTAL SALARY
13
SQL SELECT DISTINCT

Example 8: Write a query to count employee salary from employees table using distinct keyword.

SELECT COUNT(DISTINCT SALARY) AS TOTAL SALARY FROM EMPLOYEES;

In the above statement, we have counted the total number of salaries from the employee's table, excluding duplicate values.

The output for the following statement:

TOTAL SALARY
8
SQL SELECT DISTINCT

Related Topics

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.

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.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 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 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 Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

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.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

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

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 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 LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL INSERT Statement

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

5 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

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

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.