← Back to SQL, page 7

sql-0128 · Easy fresher

What rules must the SELECT lists on each side of a UNION follow, and where do the column names come from?

Both sides need the same number of columns, and matching positions need compatible types. Columns are matched by position, not by name. The result takes its column names from the first SELECT, and ORDER BY applies to the whole combined result.

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);
sql
SELECT name AS who, 'student' AS kind FROM students WHERE id <= 2
UNION ALL
SELECT title AS title, 'course' FROM courses WHERE id = 10
ORDER BY kind, who;
whokind
Databasescourse
Ashastudent
Benstudent

The output column is called who because that alias is on the first branch. The second branch's alias title is ignored. Columns line up by position, so swapping column order in one branch silently mixes data rather than raising an error.

A different column count is an error on both engines:

sql
SELECT name, city FROM students
UNION
SELECT title FROM courses;
-- error on purpose
text
Error: SELECTs to the left and right of UNION do not have the same number of result columns
sql
SELECT name, city FROM students
UNION
SELECT title FROM courses;
-- error on purpose
text
Error: each UNION query must have the same number of columns

Types are handled differently. SQLite has dynamic typing and accepts text and integers in one output column:

sql
SELECT name FROM students WHERE id = 1
UNION ALL
SELECT credits FROM courses WHERE id = 10
ORDER BY 1;
name
4
Asha

PostgreSQL requires compatible types per column and rejects the same query:

sql
SELECT name FROM students WHERE id = 1
UNION ALL
SELECT credits FROM courses WHERE id = 10;
-- error on purpose
text
Error: UNION types text and integer cannot be matched

One ORDER BY at the end sorts the combined result. To sort or limit a single branch, wrap it in a subquery.

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 name AS who, 'student' AS kind FROM students WHERE id <= 2
UNION ALL
SELECT title AS title, 'course' FROM courses WHERE id = 10
ORDER BY kind, who;

-- error on purpose
SELECT name, city FROM students
UNION
SELECT title FROM courses;

SELECT name FROM students WHERE id = 1
UNION ALL
SELECT credits FROM courses WHERE id = 10
ORDER BY 1;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Same number of columns on every branch
  • Columns match by position, not name
  • Names come from the first SELECT
  • One ORDER BY at the end sorts the whole result

Common mistakes

  • Listing columns in a different order in one branch
  • Putting ORDER BY inside a branch without a subquery

Follow-ups

  • How would you limit each branch separately?
  • Why does PostgreSQL reject text UNION integer?

Tips

  • Mention that the first branch names the columns