SQL DISTINCT keyword is used to retrieve unique ( distinct ) values from the table. If there are duplicate values present for a particular column, then SQL “distinct” keyword can be used to select only the distinct values.
SQL DISTINCT Syntax
SELECT DISTINCT column1, column2, columnN ..... FROM table_name;
where column1, column2, columnN are the column names of the table.
In this post, we are going to look at the below things one by one
|1||Peter Clark||90000.00||Daphne Road Manukau||Manager||36|
|2||Alfredo Smith||60000.00||Pimpama Drive Rotorua||Principal Engineer||32|
|3||James Cook||40500.00||Saint-Antoine Quebec||SDE I||27|
|4||Hudson Stewart||45000.00||Stoney Creek Ontario||SDE II||30|
|5||Nathan Taylor||55000.00||Long Street, Woolston||SDE II||31|
|6||Michal Clarke||53000.00||Baker Street, London||Principal Engineer||30|
|7||William Johnson||60000.00||Charlton Road, Sturbridge||Principal Engineer||35|
Using DISTINCT on only one column
The following statement will display all of the DESIGNATION column values from the employee table.
SELECT DESIGNATION FROM Employee;
The following statement will display only the unique DESIGNATION from the employee table.
SELECT DISTINCT DESIGNATION FROM Employee;
Using DISTINCT on multiple columns
select SALARY, DESIGNATION from employee;
Now, we will try to use the DISTINCT keyword on two of the columns ( SALARY and DESIGNATION ).
select DISTINCT SALARY, DESIGNATION from employee;
Here, we can see that the number of rows has been decreased by 1 after using the DISTINCT keyword. This is because the uniqueness will now be defined by the two columns combined ( Salary and Designation ) and that is the reason the row containing Principal Engineer designation and 60000.00 salary is printed only once, even though it was present two times in the actual table.
Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]