The DISTINCT keyword in SQL is used to remove duplicate rows from the result set of a query. It ensures that the query returns only unique records for the specified columns.
When a SELECT query retrieves data, there may be duplicate rows in the output. By adding the DISTINCT keyword, SQL filters out these duplicates, keeping only one occurrence of each unique combination of values in the specified columns.
SELECT DISTINCT column1, column2, ... FROM table_name;
Consider a table Employees:
EmployeeID | Department |
---|---|
1 | HR |
2 | IT |
3 | HR |
4 | Sales |
Query:
SELECT DISTINCT Department FROM Employees;
Department |
---|
HR |
IT |
Sales |
Here, duplicates in the Department column are removed.
OrderID | CustomerID | ProductID |
---|---|---|
101 | 1 | A |
102 | 1 | B |
103 | 1 | A |
104 | 2 | C |
Query:
SELECT DISTINCT CustomerID, ProductID FROM Orders;
CustomerID | ProductID |
---|---|
1 | A |
1 | B |
2 | C |
Here, DISTINCT filters out duplicate rows based on the combination of CustomerID and ProductID.
To Find Unique Values:
When you want to know all the unique values in a column or combination of columns.
Example: Listing all distinct product categories in an inventory.
To Eliminate Redundancy:
When duplicate rows in the result set are not needed for analysis or reporting.
Example: Fetching unique department names from an employee database.
For Data Cleanup:
Helps in preparing clean datasets by removing duplicates.
Performance Impact:
Using DISTINCT can slow down queries, especially on large datasets, as SQL must scan and compare rows to filter duplicates.
Not for Conditional Deduplication:
If you want conditional deduplication (e.g., keeping the latest row for each unique value), you need other techniques like ROW_NUMBER().
The DISTINCT keyword is a simple yet powerful tool to eliminate duplicate rows in SQL query results. It ensures that your output contains only unique values, making it ideal for data reporting and analysis tasks. However, use it judiciously to balance performance and accuracy.
The above is the detailed content of Mastering SQL DISTINCT: Removing Duplicates Made Simple. For more information, please follow other related articles on the PHP Chinese website!