Untitled
unknown
plain_text
2 years ago
3.7 kB
17
Indexable
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 ;
-------------------------------------------------------------------------------------
toaday assignment
From the following tables, write a SQL query to find distinct salespeople and their cities. Return salesperson ID and city.
Sample table: Salesman
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12
Sample table: Customer
customer_id | cust_name | city | grade | salesman_id
-------------+----------------+------------+-------+-------------
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005
Sample Output:
salesman_id city
5001 New York
5002 London
5002 California
5006 Paris
.....
Editor is loading...
Leave a Comment