Quick Reach
The distinct clause of MySQL
A few main points about distinct MySQL clause are:
- In order to get unique records from a table, we use Distinct clause in MySQL.
- The duplicate rows will be removed as using Distinct clause.
- The Distinct clause is used in the Select statement of MySQL.
An example of DISTINCT clause
MySQL Distinct clause syntax
Following is the general syntax to use the distinct clause:
Select Distinct column1, Column 2 from table_name
Where col1=val1 ;
One or more columns can be used in the Select – Distinct query.
Distinct clause examples
We have created a table, tbl_emp_salary_paid, which is used to store salaries of employees. You can see the output by clicking the link with each example.
MySQL select with distinct example
In our example table, we have entered duplicate employee names to enter salaries. Click on the link below to see data in the table where you can see employee names are repeating.
See Table before Distinct clause
Now we will use the Distinct clause to retrieve only unique employee names in our example table.
The select distinct statement will be:
1
|
select distinct emp_name from tbl_emp_salary_paid
|
The distinct with count example
In the following example, we will use the Distinct clause with MySQL count function. The distinct clause will only return unique employee names while count function is used to return the total number of unique employee names.
The Select Distinct query with count is:
1
|
select count(DISTINCT emp_name) as Total_unique_employees from tbl_emp_salary_paid
|
Also see MySQL count
Leave A Comment?