The DISTINCT keyword in MySQL is used to remove duplicate records from the results of a SELECT query. It ensures that the query returns only unique values in the specified columns.
Usage
The DISTINCT keyword is employed when you want to ensure that the query results contain only unique entries, eliminating any duplicate rows. It is placed directly after the SELECT keyword.
SELECTDISTINCT column1, column2,...FROM table_name;Funcționează cu
In this syntax, DISTINCT is applied to the combination of column(s) specified, ensuring only unique combinations of values are returned.
This query retrieves a list of unique countries from the customers table, eliminating any duplicates.
2. Multiple Columns
SELECTDISTINCT first_name, last_name
FROM employees;Funcționează cu
In this example, the DISTINCT keyword ensures that the combination of first_name and last_name is unique in the results, which may still include rows with the same first name but different last names, and vice versa.
3. Combined with WHERE Clause
SELECTDISTINCT category
FROM products
WHERE price >50;Funcționează cu
Here, the DISTINCT keyword is used with a WHERE clause to find unique product categories where the price is greater than 50, enhancing both the specificity and uniqueness of the results.
4. Combined with ORDER BY
SELECTDISTINCT department
FROM employees
ORDERBY department;Funcționează cu
This example demonstrates using DISTINCT with ORDER BY to retrieve unique department names and maintain a specific order in the results.
Tips and Best Practices
Use DISTINCT with care. While DISTINCT removes duplicates, it can impact performance, especially in large datasets.
Apply to necessary columns. Use DISTINCT only on columns where uniqueness is required to avoid unnecessary overhead.
Combine with ORDER BY. When necessary, combine DISTINCT with ORDER BY to maintain a specific order in results.
Be aware of NULLs.DISTINCT treats NULL values as equal, so only one NULL will be returned for each combination of columns, which can affect results if multiple NULLs are present across multiple columns.
The
DISTINCTkeyword in MySQL is used to remove duplicate records from the results of aSELECTquery. It ensures that the query returns only unique values in the specified columns.Usage
The
DISTINCTkeyword is employed when you want to ensure that the query results contain only unique entries, eliminating any duplicate rows. It is placed directly after theSELECTkeyword.In this syntax,
DISTINCTis applied to the combination of column(s) specified, ensuring only unique combinations of values are returned.Examples
1. Basic Usage
This query retrieves a list of unique countries from the
customerstable, eliminating any duplicates.2. Multiple Columns
In this example, the
DISTINCTkeyword ensures that the combination offirst_nameandlast_nameis unique in the results, which may still include rows with the same first name but different last names, and vice versa.3. Combined with WHERE Clause
Here, the
DISTINCTkeyword is used with aWHEREclause to find unique product categories where the price is greater than 50, enhancing both the specificity and uniqueness of the results.4. Combined with ORDER BY
This example demonstrates using
DISTINCTwithORDER BYto retrieve unique department names and maintain a specific order in the results.Tips and Best Practices
DISTINCTremoves duplicates, it can impact performance, especially in large datasets.DISTINCTonly on columns where uniqueness is required to avoid unnecessary overhead.ORDER BY. When necessary, combineDISTINCTwithORDER BYto maintain a specific order in results.DISTINCTtreatsNULLvalues as equal, so only oneNULLwill be returned for each combination of columns, which can affect results if multipleNULLs are present across multiple columns.