Union vs Join — SQL Technical Interview Questions & Answers

Union vs Join

Union vs Join

  • JOIN adds columns (wider row): name next to marks. UNION stacks rows (longer list): 2024 names plus 2025 names. JOIN needs a key. UNION needs the same number of columns and similar types.
  • UNION removes duplicates. UNION ALL keeps them and is faster. Column names in the result come from the first SELECT.
  • Trap: UNION when you meant JOIN (you get two lists, not a combined report). Trap: JOIN when you meant UNION (you invent a fake key).
  • On the example next to this theory: Union vs Join — jOIN combines rows using a match condition between tables.

Technical Interview Tip: JOIN widens. UNION lengthens. Draw both.

Code Examples for Union vs Join

Union vs Join — sample query

CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER, total INTEGER);
INSERT INTO customers VALUES (1, 'Asha'), (2, 'Ravi');
INSERT INTO orders VALUES (10, 1, 500), (11, 1, 200);
SELECT c.name, o.total
FROM customers c
JOIN orders o ON o.customer_id = c.id;

Union vs Join — jOIN combines rows using a match condition between tables.

How to Explain Union vs Join to an Interviewer

JOIN vs UNION?

JOIN combines related columns UNION stacks result sets