1. Syntax of Distinct
SELECT DISTINCT column_name(s)
FROM table_name;
DISTINCT column_name
: Ensures only unique values are returned from the specified column.
Let’s assume we have a table employees
with a column department
. We want to retrieve the unique department names.
SELECT DISTINCT department
FROM employees;
In this example:
To retrieve unique combinations of multiple columns, use DISTINCT on multiple fields.
SELECT DISTINCT department, job_title
FROM employees;
department
and job_title
.DISTINCT
removes duplicate values from the result set.The DISTINCT
keyword in MySQL is an essential tool for filtering unique values, ensuring efficient data retrieval without unnecessary duplication. It is especially useful in reporting and analytics.
.