×

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

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

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

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

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

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

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

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

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

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

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.