← Back to SQL, page 7

sql-0127 · Easy fresher

How do UNION and UNION ALL behave differently, and which should be the default choice?

UNION combines two results and removes duplicate rows; UNION ALL keeps every row. Removing duplicates needs a sort or hash step, so UNION ALL is cheaper. Use UNION ALL unless duplicates are actually possible and unwanted.

Full answer
sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

Cities of students in year 1, combined with cities of students in year 2:

sql
SELECT city FROM students WHERE year = 1
UNION ALL
SELECT city FROM students WHERE year = 2
ORDER BY city;
city
Leeds
Leeds
Pune
Pune
sql
SELECT city FROM students WHERE year = 1
UNION
SELECT city FROM students WHERE year = 2
ORDER BY city;
city
Leeds
Pune

UNION ALL returns all four rows, including Pune twice (Asha and Chen both live there). UNION keeps each distinct row once, so Pune and Leeds appear a single time.

Duplicate removal compares whole rows, not single columns, and it treats two NULLs as equal, unlike =. It also costs extra work on large inputs. A common habit is to write UNION ALL by default and switch to UNION only when the question says "distinct". When the two branches cannot overlap, for example rows from separate years tagged with a constant column, UNION does pointless work.

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

SELECT city FROM students WHERE year = 1
UNION ALL
SELECT city FROM students WHERE year = 2
ORDER BY city;

SELECT city FROM students WHERE year = 1
UNION
SELECT city FROM students WHERE year = 2
ORDER BY city;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • UNION removes duplicate rows; UNION ALL keeps them
  • Duplicate detection uses whole rows and treats NULLs as equal
  • UNION ALL avoids a sort or hash step
  • Prefer UNION ALL when the branches cannot overlap

Common mistakes

  • Using UNION by habit and paying for deduplication on large data
  • Expecting UNION to deduplicate on one column only

Follow-ups

  • What rules must the two SELECT lists follow?
  • How would you tag which branch each row came from?

Tips

  • State the cost difference, not just the output difference