What are set operators (Union) and how they differ from Joins in SQL

What are set operators (Union) and how they differ from Joins in SQL
What are set operators (Union) and how they differ from Joins in SQL

In this article we will analyze the use of set operators, the types that exist and the differences between them and SQL Joins.

In her world SQL (Structured Query Language) many times we will want to combine two different data sets into a new one. This is achieved by using set operators / union operator.

How is it different with SQL Joins?

Sta SQL Joins we compare columns from two different tables by making records consisting of the columns of those two tables.

Sta Union operators we join the records (lines) of two different result sets (queries) into a single one.

Set operators

There are four kinds of set operators: Union all, Union, Except, Intersect

Depending on which one we use will return a different result.

Let's explain them one by one with examples.

To begin with, we will make two tables with data so that it is easier to understand the difference between each type:

create table pelatis (
  onoma_pelati varchar(50));

create table pwlitis (
  onoma_pwliti varchar(50));
  
insert into pelatis values
('Stratos'),
('Nikos'),
('Iasonas');

insert into pwlitis values
('Stratos'),
('Giorgos'),
('Stefanos');
select * from pelatis;
select * from pwlitis;
What are set operators (Union) and how they differ from Joins in SQL

Union All

In the Union All operator, we combine the results from several queries into a single one that also contains all records that are entirely duplicates.

What are set operators (Union) and how they differ from Joins in SQL
select * from pelatis
union all
select * from pwlitis;
What are set operators (Union) and how they differ from Joins in SQL

We see that “Stratos” appears twice as it exists in both sets of results.

Union

In the Union operator as opposed to Union All when we combine the results from several queries into a single one, not contains as many records as are entirely duplicates.

select * from pelatis
union
select * from pwlitis;
What are set operators (Union) and how they differ from Joins in SQL

We see this time that “Stratos” only exists once, even though it exists in both sets of results.

Except

In the Except operator displays the first set of results after the results of the second have been removed.

What are set operators (Union) and how they differ from Joins in SQL
select * from pelatis
except
select * from pwlitis;
What are set operators (Union) and how they differ from Joins in SQL

Does not appear "Stratos" as it was present in the second set of results.

Intersect

The Intersect operator only displays those records that are the same in both result sets.

What are set operators (Union) and how they differ from Joins in SQL
select * from pelatis
intersect
select * from pwlitis;
What are set operators (Union) and how they differ from Joins in SQL

In this case only the registration "Stratos" is common to both sets of results.

Normal uses:

  • The number of columns and their order should be the same in all queries.
  • The data types should be compatible. (i.e. it is not possible to define the phone as text in the first query and as an integer in the second)

Sources:

Share it

Leave a reply