MySQL Distinct: 3 examples to use in Select statement

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:

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:

Also see MySQL count

Was this article helpful?

Related Articles

Leave A Comment?