MySQL Distinct

The MySQL Select Distinct Statement is used to retrieve unique rows or eliminating the duplicate rows from the specified Column in the SELECT Statement.

MySQL Select Distinct Syntax:

The basic syntax of the Select Distinct in MySQL can be written as:

SELECT DISTINCT [Columns]
FROM Source
WHERE Conditions — This is Optional

DISTINCT: This keyword returns Unique Columns
Columns: It allows us to choose the number of columns you want to use from the table. It may be One or more.
Source: One or more tables present in the Database. Use Joins to join multiple table.

SQL DISTINCT on Single Column:
When we use MySQL Distinct on Single columns then the SELECT Statement will write the unique individual records.

-- MySQL SELECT DISTINCT Example:
SELECT DISTINCT RollNumber
FROM students;

MySQL Distinct on Multiple Columns:

When we use MySQL Distinct on multiple columns then the SELECT Statement will write the unique combination of multiple columns instead of unique individual records.

-- MySQL SELECT DISTINCT Example

SELECT DISTINCT RollNumber, Subject
FROM students
ORDER BY Rollnaumber, Subject;