In this Tutorial, we will learn how to SELECT DISTINCT
records from the table using SQL.
The SELECT DISTINCT
Statement is used to eliminate all the duplicate records and fetch/retrieve only unique records from the table present in the database.
SELECT DISTINCT column1, column2,...columnN
FROM tableName;
Let us consider this table "Employee" for records.
Table Name: Employee
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
5 | Shalini | Jaipur | India | female | 18000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
8 | Sarah | New York | US | female | 76000 |
9 | Rocky | Noida | India | male | 28000 |
Here are some examples of SELECT DISTINCT
statement:
First, lets us see how to SELECT Statement returns the duplicate "City" Records from table "Employee"
SELECT City FROM Employee;
City |
Delhi |
Delhi |
Mumbai |
Noida |
Jaipur |
Faridabad |
Mumbai |
New York |
Noida |
Now, lets us see how to SELECT DISTINCT Statement returns the "City" Records from table "Employee"
SELECT DISTINCT City
FROM Employee;
City |
Delhi |
Mumbai |
Noida |
Jaipur |
Faridabad |
New York |