DISTINCT keyword in SQL

The DISTINCT keyword in SQL is used to remove duplicate rows from a result set. This is useful when we want to retrieve only unique values from a column or a combination of columns.

SQL Syntax

SELECT DISTINCT column_name
FROM table_name;

This query will return only the unique values in the specified column.

Example: Let's create a stored procedure that retrieves all employees from the employees table.

Table: employees

EmployeeIDNameAgeSalaryDepartment
1Ashish2425000Electrical
2Kirshan3230000Electrical
3Anjali2025000Electronics
4Manjulika3015000Electrical
5Katrina3718000Computer
6Esha3720000Computer
7Ankita209000Electronics
8Meenakshi3015000Computer
9Alia3716000Electronics

With One Column Here we are finding the distinct values in one column.

SQL

SELECT DISTINCT department
FROM employees;

The result of the above query is shown below:

Department
Computer
Electrical
Electronics