Untitled

 avatar
unknown
plain_text
a year ago
3.7 kB
17
Indexable
12/03/2024
========================================================================================
union and union 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

exa:
use testdb;
select * from customer;
select * from customerinfo;

select * from customer
union
select * from customerinfo;

select customer_id,cust_name from customer
union
select cust_id,cust_name from customerinfo;

select * from customer
union
select * from customerinfo where age>50
order by city desc;

--------------------------------------------------------------------------------------------------------
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 customer
union all
select * from customerinfo;
select customer_id,cust_name from customer
union all
select cust_id,cust_name from customerinfo;

===================================================================================================

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


===================================================================================================================

Views:-

View:
view is virtual table .
view conatains rows and column same as real table.
we can create view by selecting rows from one or more table.

synatx : create view view_name as
select columns from tablename.

exa:-
create view customer_view as
select * from customer;

create or replace view customer_view as 
select customer_id,cust_name from customer where city ="pune";

select * from customer_view;

create or replace view custinfo_view as 
select c.customer_id,c.cust_name,ci.cust_id,ci.age from customer c ,customerinfo ci 
where c.cust_name=ci.cust_name;

select * from custinfo_view


for delete the view 
syntax: drop view viewname;
drop view custinfo_view;

Editor is loading...
Leave a Comment