T-SQL Tutorial

Difference between UNION and UNION ALL


UNION vs UNION ALL

UNION returns only distinct values. The UNION operator eliminate duplicate rows.
UNION ALL returns all values. The UNION ALL operator will not eliminate duplicate rows.

Students table:

IDNAMECITY
1EmmaNew York
2DanielChicago
3JosephDallas




UNION Example

select id, name, city
from Students where id=1
union
select id, name, city
from Students where id=1;

Results

IDNAMECITY
1EmmaNew York

UNION ALL Example

select id, name, city
from Students where id=1
union all
select id, name, city
from Students where id=1;

Results

IDNAMECITY
1EmmaNew York
1EmmaNew York