In MySQL, the DISTINCT keyword is used in the SELECT clause of a query to eliminate duplicate rows from the results. For example, the following query uses DISTINCT to return only unique values from the “department” column:
SELECT DISTINCT department FROM employees;
This query will return a list of all the unique departments in the “employees” table, without any duplicate values.
The DISTINCT keyword can be used in combination with other clauses and keywords in a query, such as WHERE, GROUP BY, and ORDER BY. For example, the following query uses DISTINCT with a WHERE clause to return only unique departments that have at least 10 employees:
SELECT DISTINCT department
FROM employees
WHERE employees.department IN (
SELECT department
FROM employees
GROUP BY department
HAVING COUNT(*) >= 10
);
Overall, the DISTINCT keyword is a useful tool for eliminating duplicate rows from the results of a MySQL query. It can be used in a variety of situations to help you retrieve and analyze the unique values in your data.