What is SELECT DISTINCT ?
Answer : The SQL SELECT DISTINCT statement is used to return only distinct(unique) records. There May be a situation when you have a multiple duplicate records in a table. While fetching such records, it makes more sense to fetch only those unique records instead of fetching duplicate records.
Syntax
SELECT DISTINCT column_name
FROM table_name;
Example
Suppose we have an Employees table:
| Employee_ID | Employee_Name | Department |
|---|---|---|
| 101 | Rahul | IT |
| 102 | Priya | HR |
| 103 | Amit | Finance |
| 104 | John | Finance |
If we want to fetch the unique departments:
SELECT DISTINCT Department
FROM Employees;
Output
| Department |
|---|
| IT |
| HR |
| Finance |
Here, Finance appears twice in the table, but SELECT DISTINCT returns it only once.