MYSQL Select Distinct Statement
The distinct
keyword in select statement is used retrieve only the distinct values in the table.
Syntax
select distinct column_1, column_2 from table_name;
Let's assume that we have a table employee with the following data.
empno | name | age | role | location |
---|---|---|---|---|
001 | Andrew | 30 | Manager | India |
002 | Beslin | 28 | Business Analyst | India |
003 | Joanna | 23 | Senior Developer | USA |
Example
select location from employee;
This will return
location |
---|
India |
India |
USA |
In order to retrieve location without duplicate entry, we use the following query.
select distinct location from employee;
Output
location |
---|
India |
USA |
To retrieve the distinct count of the location we use
select count(distinct location) from employee;
This will return 2 as output.