Difference between `union` and `union all` in psql
union
is an operator that operates on two sets of results, combining them:
chris=# select 1 union select 2;
?column?
----------
1
2
(2 rows)
It filters duplicate results though:
chris=# select 1 union select 1;
?column?
----------
1
(1 row)
If you want to not worry about duplicate results and just stack the result sets on top of each other use union all
:
chris=# select 1 union all select 1;
?column?
----------
1
1
(2 rows)
Tweet