Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
2.2 kB
6
Indexable
Never
01/02/2024
---------------------------------------------------------------------------------------
Union operator and unio all operator

Union operator:
Union operator is used to combine the resultset of two or more tables:
union operator select only distinct values.
rules :

-number of columns same in both table.
-the columns must be same datatype.
-the column in every select clause must also in the same order and number of column must be same

syntax:- 
select columns from table1
union
select columns from table2

-- create table employee1(id int ,name varchar(30),age int ,salary int ,city varchar(30));
-- create table manager(id int ,name varchar(30),age int ,salary int ,city varchar(30));

insert into employee1 values(1,"ram",56,670000,"pune"),
(2,"sham",45,70000,"satara"),
(3,"gita",57,670000,"satara"),
(4,"sita",89,780000,"pune")

insert into manager values(1,"jiyu",78,200000,"nagpur"),
(2,"ajali",67,70000,"satara"),
(3,"nita",23,30000,"nashik"),
(4,"sita",89,780000,"pune")

select * from employee1
union
select * from manager;

select name,age from employee1
union
select name,age from manager;

select * from employee1 where salary >40000
union
select * from manager where salary >40000;

select * from employee1 where salary >(select salary from employeeinfo where id=3)
union
select * from manager where salary >40000;

select customer_id as id, age ,first_name as name from customers 
union 
select id,age,name from manager ;

--------------------------------------------------------------------------------------
union all:

Union operator is used to combine the resultset of two or more tables:
union all select all rows including duplicate.
rules :

-number of columns same in both table.
-the columns must be same datatype.
-the column in every select clause must also in the same order and number of column must be same

syntax:- 
select columns from table1
union all
select columns from table2


select * from employee1 
union all
select * from manager ;

select customer_id as id, age ,first_name as name from customers 
union all
select id,age,name from manager ;


Leave a Comment