Beginner

451. JOIN with DISTINCT

Learn how DISTINCT can remove duplicate result combinations after a JOIN.

📘 Lesson 451 of 728 ⏱ 11 min 🎯 Intermediate

DISTINCT removes duplicate rows from the final SELECT result. When used with JOIN, it can return only unique combinations of the selected columns.

JOINs can produce repeated combinations when one table contains multiple matching records. DISTINCT can remove repeated result rows when duplicates are not meaningful.

Return each customer only once even when the customer has multiple orders.

SQL
SELECT DISTINCT
    c.CustomerID,
    c.CustomerName
FROM Customers c
INNER JOIN Orders o
    ON c.CustomerID = o.CustomerID;

DISTINCT with JOIN is useful for customer lists, unique product lists, reporting, and data validation.

  • DISTINCT removes duplicate result rows.
  • DISTINCT applies to all selected columns together.
  • It can simplify results after a JOIN.
  • DISTINCT does not fix an incorrect JOIN.
  • Use it only when duplicate result combinations are genuinely unwanted.
Can DISTINCT be used with JOIN?

Yes. DISTINCT can remove duplicate combinations from the result produced by a JOIN.

Does DISTINCT fix a wrong JOIN condition?

No. DISTINCT only removes identical result rows; it does not correct an incorrect relationship.

Have you finished this lesson?

Introduction to SQL – All Lessons