UNION vs UNION ALL in SQL

Last Updated : 3 Sep, 2026

UNION and UNION ALL are SQL operators used to combine the results of two or more SELECT statements into a single result set. The main difference is how they handle duplicate rows.

  • UNION combines the results and removes duplicate rows.
  • UNION ALL combines the results and keeps duplicate rows.

Difference Between UNION and UNION ALL

The table below shows the major differences between UNION and UNION ALL:

UNIONUNION ALL
Removes duplicate rows from the result.Keeps duplicate rows in the result.
Generally slower because duplicate rows are removed.Generally faster because duplicates are not removed.
Used when unique results are required.Used when all rows are required.
The SELECT statements must have the same number of columns.The SELECT statements must have the same number of columns.
Corresponding columns must have compatible data types.Corresponding columns must have compatible data types.
Example: SELECT name FROM employee_2024 UNION SELECT name FROM employee_2025;Example: SELECT name FROM employee_2024 UNION ALL SELECT name FROM employee_2025;

Example of UNION and UNION ALL

The following examples demonstrate how UNION and UNION ALL combine results and handle duplicate rows.

emp_1 table

Screenshot-2026-09-02-102901

emp_2 table

Screenshot-2026-09-02-103032

UNION

Query:

SELECT employee_id, nameFROM emp_1UNIONSELECT employee_id, nameFROM emp_2;

Output:

Screenshot-2026-09-02-103109
  • Here, the duplicate row for Bob appears only once because UNION removes duplicate rows.

UNION ALL

Query:

SELECT employee_id, nameFROM emp_1UNION ALLSELECT employee_id, nameFROM emp_2;

Output:

Screenshot-2026-09-02-103359
  • Here, the duplicate row for Bob appears twice because UNION ALL retains all rows.
Comment