Used with the SELECT statement, the DISTINCT clause is used to fetch unique records from the table and remove duplicate records.
Syntax
SELECT DISTINCT column_name
FROM table_name
[WHERE conditions];
Distinct MySQL Example
Let us see a real-time example of how this works. For this, we are going to use the info table.
Let us fetch the unique cities alone without duplicates.
select distinct city from info;
We can see that it retrieved the unique cities among the duplicates.
We will be fetching a unique city out of the info and adding the Name expression.
select distinct city, Name from info;
We can see that we have passed in multiple distinct expressions. In this case, the column names and retrieved the unique record of city people are from.
This is how we can use the DISTINCT clause effectively.
- Log in to post comments