Beginner

692. JOIN with Set Operations

Combine JOINs with UNION, INTERSECT, and EXCEPT to solve complex data-matching problems.

📘 Lesson 692 of 728 ⏱ 16 min 🎯 Advanced

Set operations combine query results vertically or logically, while JOINs connect related columns between datasets.

Complex reporting often requires both row-set comparison and relational matching.

Combine customer IDs from two sources and then retrieve customer details.

SQL
SELECT
    c.CustomerID,
    c.CustomerName
FROM Customers c
INNER JOIN (
    SELECT CustomerID
    FROM OnlineCustomers

    UNION

    SELECT CustomerID
    FROM BranchCustomers
) x
    ON c.CustomerID = x.CustomerID;

Used in data migration, reconciliation, enterprise reporting, and master-data management.

  • Set operations combine result sets.
  • JOIN connects rows through related columns.
  • UNION combines rows from multiple queries.
  • INTERSECT finds common rows.
  • EXCEPT finds rows missing from the second result.
How are JOINs different from set operations?

JOINs combine columns horizontally using relationships, while set operations combine compatible result rows vertically.

Can JOIN and UNION be used together?

Yes. A UNION result can be treated as a derived dataset and JOINed with another table.

Have you finished this lesson?

Introduction to SQL – All Lessons