MySQL – Distinct Values

A column in a MySQL Table can have duplicate values based on the constraints, etc.

In this tutorial, we will learn how to get distinct values present in a specific column of a given MySQL Table.

To get unique or distinct values of a column in MySQL Table, use the following SQL Query.

SELECT DISTINCT(column_name) FROM your_table_name;

You can select distinct values for one or more columns. The column names has to be separated with comma.

SELECT DISTINCT column_name_1, column_name_2 FROM your_table_name;

The above query selects minimum number of rows that has unique values for each column specified in the query.

Example 1 – Get Distinct Values of a Column

In this example, we shall consider the following table data.

ADVERTISEMENT
MySQL - Select DISTINCT values of a column

In the above table

  • For age column, there are two distinct values 9, 10.
  • For section column, there are three distinct values A, B, C.
  • For gender column, there are two distinct values M, F.

Now, we shall write SQL Queries to get distinct values for these columns.

Get DISTINCT values for age column

SELECT DISTINCT(age) FROM students;
MySQL SELECT DISTINCT values of a column

Get DISTINCT values for section column

SELECT DISTINCT section FROM students;
SELECT distinct values of column in MySQL

Get DISTINCT values for two columns

SELECT DISTINCT section, age FROM students;
MySQL SELECT DISTINCT values of multiple colulmns

Conclusion

In this MySQL Tutorial, we have learnt to get distinct values of one or more columns.