MySQL DISTINCT

Assume in a table, some of the columns may contain duplicate values. This is not a problem; however, sometimes you will want to list only the different (distinct) values in a table.

DISTINCT helps to eliminate duplicates. If a query returns a result that contains duplicate rows, you can remove duplicates to produce a result set in which every row is unique. To do this, include the keyword DISTINCT after SELECT and before the output column list.

Syntax :

SELECT DISTINCT field_name FROM table_name

Example:

Consider this below employee table

Id Name Age
1 James 25
2 John 26
3 Robert 25

Now we want to select only the distinct values from the column named “age” from the table above.

We use the following SELECT query:

<?php
SELECT DISTINCT age FROM employee;
?>

The result-set look like this

age
25
26
Posted in MySQL