MySQL DISTINCT clause is used in MySQL queries to filter the data after removing the duplicate records. By using DISTINCT clause, you will fetch only the unique records from the database table. In this tutorial, I will share MySQL Distinct clause with examples.
Below is the syntax of writing MySQL DISTINCT clause in your MySQL queries.
1 2 3 | SELECT DISTINCT field_name FROM tables_name [WHERE conditions]; |
Example 1
Below query will fetch the unique records from the employee table basis on employee name.
1 2 | SELECT DISTINCT emp_name FROM employee; |
Example 2
Another example with two fields to fetch records from the table.
1 2 | SELECT DISTINCT emp_name, dept_name FROM employee; |
Example 3
A distinct clause with WHERE statement.
1 2 | SELECT DISTINCT emp_name, dept_name FROM employee WHERE emp_skill='JAVA'; |