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.
In the above table
agecolumn, there are two distinct values
sectioncolumn, there are three distinct values
A, B, C.
gendercolumn, there are two distinct values
Now, we shall write SQL Queries to get distinct values for these columns.
Get DISTINCT values for age column
SELECT DISTINCT(age) FROM students;
Get DISTINCT values for section column
SELECT DISTINCT section FROM students;
Get DISTINCT values for two columns
SELECT DISTINCT section, age FROM students;
In this MySQL Tutorial, we have learnt to get distinct values of one or more columns.