The distinct keyword in SQL is used alongside the SELECT statement to retrieve unique values from the table. The Distinct keyword removes all the duplicate records while retrieving records from any table in the database.
Below is the syntax for distinct keyword
SELECT DISTINCT column-name FROM table-name;
Let’s also consider the Emp table. you can see in the table below, that there is an employee name, employee salary, and also their age.
More so, multiple employees have the same salary as shown in the table below; therefore, we can use the DISTINCT keyword to list down distinct salary amount which is currently paid to the employees.
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 5000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 10000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
SELECT DISTINCT salary FROM Emp;
So, therefore, the SQL query above will return only the unique salary from the Emp table.
salary |
---|
5000 |
8000 |
10000 |