MySQL distinct
About 304 wordsAbout 1 minDecember 4, 2024
The DISTINCT
keyword is typically used with the SELECT
statement to ensure that each row in the query result is unique.
Usage with Multiple Columns
The DISTINCT
keyword can be used on multiple columns. In this case, only rows where all specified columns are the same will be considered duplicates and removed.
SELECT DISTINCT name, email FROM users;
Considerations
- Performance: Using the
DISTINCT
keyword can impact query performance, as MySQL needs to perform additional operations to eliminate duplicate rows. For large datasets, it should be used cautiously and the query performance should be tested. - NULL Values:
DISTINCT
treatsNULL
values as a unique value. For instance, if a column contains multipleNULL
values,DISTINCT
will still return oneNULL
.