DISTINCT removes duplicate combinations of the selected columns from the final result set.
JOINs can return repeated values when multiple related records exist. DISTINCT can be useful when only unique combinations are required.
Return each customer only once when identifying customers who have placed orders.
SELECT DISTINCT
c.CustomerID,
c.CustomerName
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID;
Used in customer lists, product lists, reporting, validation, and data exploration.
- DISTINCT removes duplicate result combinations.
- It works on the complete selected column combination.
- DISTINCT does not fix an incorrect JOIN.
- It may add processing overhead on large datasets.
- Use it when uniqueness is actually required.
What does DISTINCT do after a JOIN?
It removes repeated combinations of the selected columns from the final result.
Does DISTINCT fix a bad JOIN?
No. DISTINCT may hide duplicate results caused by an incorrect join condition rather than solving the underlying problem.