10-01 UNION

01

Union allows us to combine the values of columns of different tables together as long as they are of the same value type

select branch_name
from branch
UNION
select first_name
from employee;

you must have the same number of columns when select so branch_name and first_name

02

You can use UNION multiple times

select branch_name 
from branch
UNION
select client_name
from client
UNION
select supplier_name
from supplier;

03

The column name will use the first column name in the first select statement, us AS on the first selected column to change the name

select branch_name AS all_names
from branch
UNION
select client_name 
from client
UNION
select supplier_name
from branch_supplier;

04

We can add multiple columns but **remember, they must be the same number of columns and the values must be of similiar type

select branch_id AS branch_ids, client_name AS names
from client
UNION
select branch_id,supplier_name
from branch_supplier;

05

Since both the client and branch_supplier tables have a branch_id column, it's good practice to specify the table when selecting the column

select client.branch_id, client.client_name AS names
from client
UNION
select branch_supplier.branch_id, branch_supplier.supplier_name
from branch_supplier;

06

Find money spent and earned by company

select 'operating cost', SUM(salary)
from employee
UNION
select 'total sales' , SUM(total_sales)
from works_with;