Chapter 13: Set Operations

Set operations let you combine the results of two or more SELECT queries as if they were mathematical sets. Think of each SELECT as producing a “bag of rows”. With set operations, you can:

  • Merge them together
  • Find what’s common
  • Find what’s in one but not the other

The four main set operations we’ll cover today are:

  • UNION and UNION ALL
  • INTERSECT
  • EXCEPT (called MINUS in Oracle)

These are very powerful for reports, data cleaning, finding duplicates, comparing datasets, etc.

Important Rules That Apply to ALL Set Operations

  1. All SELECTs must have the same number of columns
  2. Corresponding columns must have compatible data types (e.g., INT with INT, VARCHAR with VARCHAR)
  3. Column names are taken from the FIRST SELECT (others are ignored)
  4. Order of columns matters — they must match positionally
  5. Set operations remove duplicates by default (except UNION ALL)

Let’s use our bookshop database and create two more tables for realistic examples.

Setup – Extra Tables for This Chapter

SQL

1. UNION – Combine Two Result Sets (Removes Duplicates)

UNION takes all rows from both queries and removes duplicates.

Syntax:

SQL

Example – All authors from India AND USA (no duplicates):

SQL

Result (only unique authors):

name country
Amish Tripathi India
Chetan Bhagat India
Chitra Banerjee USA
Jhumpa Lahiri USA
Khushwant Singh India
R.K. Narayan India
Ruskin Bond India
Sudha Murty India
Vikram Seth India

If an author was in both tables → appears only once.

2. UNION ALL – Combine Two Result Sets (Keeps All Duplicates)

UNION ALL is like UNION but does NOT remove duplicates → faster and keeps everything.

Example – All books from old AND recent (even if somehow duplicated):

SQL

Result: All 10 books appear, even if there was overlap (in our case, no overlap, but if there was → duplicates kept).

When to use UNION ALL instead of UNION?

  • When you know there are no duplicates (or you want to keep them)
  • When performance matters (UNION ALL is faster because no deduplication)

3. INTERSECT – Find Common Rows (What’s in Both)

INTERSECT returns only rows that appear in both result sets.

Syntax:

SQL

Example – Authors who appear in both indian_authors and us_authors (in our data: none, but imagine):

SQL

Result: Empty (no common authors)

Realistic example – Find books that are both old and recent? (none in our case):

SQL

Note: MySQL does not support INTERSECT natively until version 8.0.31+. Workaround: Use INNER JOIN or EXISTS.

Alternative in MySQL:

SQL

4. EXCEPT / MINUS – Rows in First But Not in Second

EXCEPT (SQL standard) or MINUS (Oracle) returns rows that are in the first result set but not in the second.

Syntax:

SQL

Example – Authors who are Indian but NOT in the US list:

SQL

Result: All Indian authors (since no overlap)

Another example – Old books that are NOT in recent books:

SQL

Result: All old books (The Room on the Roof, The Blue Umbrella, Malgudi Days, Train to Pakistan)

Note: MySQL does not support EXCEPT/MINUS natively. Workaround: Use LEFT JOIN + IS NULL or NOT EXISTS.

MySQL workaround for EXCEPT:

SQL

Full Realistic Business Example – Combining Everything

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *