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;
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
EmployeeID | Name | Age | Salary | Department |
---|---|---|---|---|
1 | Ashish | 24 | 25000 | Electrical |
2 | Kirshan | 32 | 30000 | Electrical |
3 | Anjali | 20 | 25000 | Electronics |
4 | Manjulika | 30 | 15000 | Electrical |
5 | Katrina | 37 | 18000 | Computer |
6 | Esha | 37 | 20000 | Computer |
7 | Ankita | 20 | 9000 | Electronics |
8 | Meenakshi | 30 | 15000 | Computer |
9 | Alia | 37 | 16000 | Electronics |
With One Column Here we are finding the distinct values in one column.
SQL
SELECT DISTINCT department
FROM employees;
FROM employees;
The result of the above query is shown below:
Department |
---|
Computer |
Electrical |
Electronics |