9 Set Aggr
9 Set Aggr
9 Set Aggr
To
implement
set
operations
and
Aggregate
: Oracle 9i
SELECT statements.Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order. UNION Syntax SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2 Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL. UNION ALL Syntax SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2 PS: The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.
Aggregate Functions :
SQL aggregate functions return a single value, calculated from values in a column. Useful aggregate functions: AVG() - Returns the average value COUNT() - Returns the number of rows MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum AVG() Function :The AVG() function returns the average value of a numeric column.
Syntax SELECT AVG(column_name) FROM table_name COUNT() Function :The COUNT() function returns the number of rows that matches a specified criteria. Syntax The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(column_name) FROM table_name MAX() Function : The MAX() function returns the largest value of the selected column. Syntax : SELECT MAX(column_name) FROM table_name MIN() Function The MIN() function returns the smallest value of the selected column. Syntax : SELECT MIN(column_name) FROM table_name SUM() Function :The SUM() function returns the total sum of a numeric column. SQL SUM() Syntax: SELECT SUM(column_name) FROM table_name Conclusion : Thus we have implemented set operations and aggregate function
Enter value for p_name: Airbus 320 Enter value for p_type: Freight Enter value for p_cost: 6000000 old 1: insert into plane2 values(&p_no, '&p_name', '&p_type', &p_cost) new 1: insert into plane2 values(369, 'Airbus 320', 'Freight', 6000000) 1 row created. SQL> / Enter value for p_no: 380 Enter value for p_name: Airbus 380 Enter value for p_type: Passenger Enter value for p_cost: 100000000 old 1: insert into plane2 values(&p_no, '&p_name', '&p_type', &p_cost) new 1: insert into plane2 values(380, 'Airbus 380', 'Passenger', 100000000) 1 row created. SQL> select * from plane1; P_NO P_NAME P_TYPE P_COST
---------- ---------- ---------- ---------147 Boeing 747 Jumbo Jet 258 Boeing 777 Passenger SQL> select * from plane2 ; P_NO P_NAME P_TYPE P_COST 10000000 5000000
380 Airbus 380 Passenger 100000000 SQL> insert into plane2 values(147, 'Airbus 330', 'Freight', 5000000); 1 row created. SQL> select * from plane2; P_NO P_NAME P_TYPE P_COST
380 Airbus 380 Passenger 100000000 147 Airbus 330 Freight 5000000
---------- ---------- ---------- ---------147 Airbus 330 Freight 147 Boeing 747 Jumbo Jet 258 Boeing 777 Passenger 369 Airbus 320 Freight 5000000 10000000 5000000 6000000
380 Airbus 380 Passenger 100000000 SQL> insert into plane2 values(258, 'Boeing 777', 'Passenger', 5000000); 1 row created. SQL> select * from plane1 union select * from plane2; P_NO P_NAME P_TYPE P_COST
---------- ---------- ---------- ---------147 Airbus 330 Freight 147 Boeing 747 Jumbo Jet 258 Boeing 777 Passenger 369 Airbus 320 Freight 5000000 10000000 5000000 6000000
380 Airbus 380 Passenger 100000000 SQL> select * from plane2 union select * from plane1; P_NO P_NAME P_TYPE P_COST
---------- ---------- ---------- ---------147 Airbus 330 Freight 147 Boeing 747 Jumbo Jet 258 Boeing 777 Passenger 369 Airbus 320 Freight 5000000 10000000 5000000 6000000
380 Airbus 380 Passenger 100000000 SQL> select p_name from plane1 union select p_name from plane2; P_NAME
---------Airbus 320 Airbus 330 Airbus 380 Boeing 747 Boeing 777
SQL> select p_no from plane1 union select p_no from plane2 P_NO ---------147 258 369 380
2. union all.
SQL>select * from plane1 union all select * from plane2 ; P_NO P_NAME P_TYPE P_COST
---------- ---------- ---------- ---------147 Boeing 747 Jumbo Jet 258 Boeing 777 Passenger 369 Airbus 320 Freight 10000000 5000000 6000000
380 Airbus 380 Passenger 100000000 147 Airbus 330 Freight 258 Boeing 777 Passenger 6 rows selected. 5000000 5000000
3. intersect.
SQL> select * from plane1 intersect select * from plane2; P_NO P_NAME P_TYPE P_COST
4. minus.
SQL> select * from plane1 minus select * from plane2;
P_NO P_NAME
P_TYPE
P_COST
380 Airbus 380 Passenger 100000000 147 Airbus 330 Freight 258 Boeing 777 Passenger 5000000 5000000
1. sum.
SQL> select sum(p_no) from plane2; SUM(P_NO) ---------1154
2. avg.
SQL> select avg(p_cost) from plane2; AVG(P_COST) ----------29000000
3. variance.
SQL> select variance(p_cost) from plane2; VARIANCE(P_COST) ---------------2.2407E+15
4. count.
5. min.
SQL> select min(p_cost) from plane2; MIN(P_COST) ----------5000000
6.max.
SQL> select max(p_cost) from plane2; MAX(P_COST) ----------100000000
7. stddev.
SQL> select stddev(p_cost) from plane2; STDDEV(P_COST) -------------47335680. CODE: SQL> create table department 2 ( 3 dept_id varchar(10)primary key, 4 name varchar(35), 5 addr varchar(45), 6* designation varchar(20))
7 ;
Table created.
SQL> 1 create table projects 2 ( 3 p_id varchar(10), 4 name varchar(10), 5 dur varchar(10), 6 dept_id varchar(10), 7* foreign key (dept_id) references department(dept_id)) 8 ;
Table created.
----------------------------------------- -------- ---------------------------DEPT_ID NAME ADDR DESIGNATION NOT NULL VARCHAR2(10) VARCHAR2(35) VARCHAR2(45) VARCHAR2(20)
DUR DEPT_ID
VARCHAR2(10) VARCHAR2(10)
Table altered.
----------------------------------------- -------- ---------------------------DEPT_ID NAME ADDR DESIGNATION P_ID NOT NULL VARCHAR2(10) VARCHAR2(35) VARCHAR2(45) VARCHAR2(20) VARCHAR2(10)
SQL> create table supplier 2 ( 3 s_id varchar(10)primary key, 4 addrs varchar(40), 5 name varchar(30), 6 demand varchar(10));
Table created.
----------------------------------------- -------- ---------------------------S_ID ADDRS NAME DEMAND NOT NULL VARCHAR2(10) VARCHAR2(40) VARCHAR2(30) VARCHAR2(10)
SQL> create table buy_order 2 ( 3 delivery_date number(10), 4 delivery varchar(20), 5 order_date number(10), 6* order_id varchar(10)primary key) SQL> /
Table created.
SQL> create table product 2 ( 3 camera varchar(10), 4 mobile varchar(10), 5 pc varchar(15), 6 order_id varchar(10), 7 foreign key (order_id) references buy_order(order_id) 8* ) SQL> /
Table created.
----------------------------------------- -------- ---------------------------DELIVERY_DATE DELIVERY ORDER_DATE ORDER_ID NUMBER(10) VARCHAR2(20) NUMBER(10) NOT NULL VARCHAR2(10)
SQL> create table stocks 2 ( 3 sto_loc varchar(35), 4 workers varchar(20), 5 quantity varchar(10));
Table created.
2 ( 3 type varchar(10));
Table created.
SQL> insert into employee 2 values(&type); Enter value for type: 'permanent' old 2: values(&type) new 2: values('permanent')
1 row created.
SQL> / Enter value for type: 'temporary' old 2: values(&type) new 2: values('temporary')
1 row created.
TYPE
---------permanent temporary
SQL> create table acc_section 2 ( 3 cash varchar(10), 4 cheque varchar(16), 5 e_pay varchar(10));
Table created.
SQL> insert into department 2* values(&dept_id,&name,&addr,&desigination,&p_id) SQL> / Enter value for dept_id: 1 Enter value for name: 'software' Enter value for addr: 'xyz' Enter value for desigination: 'asd' Enter value for p_id: 11 old 2: values(&dept_id,&name,&addr,&desigination,&p_id) new 2: values(1,'software','xyz','asd',11)
1 row created.
SQL> / Enter value for dept_id: 2 Enter value for name: 'hardware' Enter value for addr: 'abc' Enter value for desigination: 'dsg' Enter value for p_id: 22 old 2: values(&dept_id,&name,&addr,&desigination,&p_id) new 2: values(2,'hardware','abc','dsg',22)
1 row created.
SQL> / Enter value for dept_id: 3 Enter value for name: 'desk' Enter value for addr: 'pqr' Enter value for desigination: 'dfbv' Enter value for p_id: 33 old 2: values(&dept_id,&name,&addr,&desigination,&p_id) new 2: values(3,'desk','pqr','dfbv',33)
1 row created.
DEPT_ID
NAME
1 xyz
software asd 11
2 abc
hardware dsg 22
3 pqr
desk dfbv 33
SQL> insert into projects 2 values(&p_id,&name,&dur,&dept_id); Enter value for p_id: 11 Enter value for name: 'dfg' Enter value for dur: 365 Enter value for dept_id: 1 old 2: values(&p_id,&name,&dur,&dept_id) new 2: values(11,'dfg',365,1)
1 row created.
SQL> / Enter value for p_id: 22 Enter value for name: 'dgfg' Enter value for dur: 400 Enter value for dept_id: 2 old 2: values(&p_id,&name,&dur,&dept_id) new 2: values(22,'dgfg',400,2)
1 row created.
SQL> / Enter value for p_id: '33' Enter value for name: 'dsgsg' Enter value for dur: '500' Enter value for dept_id: '3' old 2: values(&p_id,&name,&dur,&dept_id) new 2: values('33','dsgsg','500','3')
1 row created.
P_ID
NAME
DUR
DEPT_ID
---------- ---------- ---------- ---------11 22 33 dfg dgfg dsgsg 365 400 500 1 2 3
SQL> insert into buy_order 2 values(&delivery_date,&delivery,&order_date,&order_id); Enter value for delivery_date: 30 Enter value for delivery: 'mobile' Enter value for order_date: 12 Enter value for order_id: '1q' old 2: values(&delivery_date,&delivery,&order_date,&order_id) new 2: values(30,'mobile',12,'1q')
1 row created.
SQL> / Enter value for delivery_date: 21 Enter value for delivery: 'pc' Enter value for order_date: 31 Enter value for order_id: '2w' old 2: values(&delivery_date,&delivery,&order_date,&order_id) new 2: values(21,'pc',31,'2w')
1 row created.
SQL> / Enter value for delivery_date: 29 Enter value for delivery: 'camera' Enter value for order_date: 12 Enter value for order_id: '3e' old 2: values(&delivery_date,&delivery,&order_date,&order_id) new 2: values(29,'camera',12,'3e')
1 row created.
SQL> insert into product 2 values(&mobile,&camera,&order_id); Enter value for mobile: 's3' Enter value for camera: '10px' Enter value for order_id: '1q' old 2: values(&mobile,&camera,&order_id) new 2: values('s3','10px','1q')
1 row created.
SQL> / Enter value for mobile: 's3' Enter value for camera: '26px' Enter value for order_id: '2w' old 2: values(&mobile,&camera,&order_id) new 2: values('s3','26px','2w')
1 row created.
SQL> / Enter value for mobile: 's' Enter value for camera: '32px' Enter value for order_id: '3e' old 2: values(&mobile,&camera,&order_id) new 2: values('s','32px','3e')
1 row created.
DELIVERY_DATE DELIVERY
ORDER_DATE ORDER_ID
CAMERA
MOBILE
ORDER_ID
no rows selected
SQL> insert into supplier 2 values(&s_id,&addr,&name,&demand); Enter value for s_id: 1 Enter value for addr: 'dsf' Enter value for name: 'asdd' Enter value for demand: '50000ps' old 2: values(&s_id,&addr,&name,&demand) new 2: values(1,'dsf','asdd','50000ps')
1 row created.
SQL> / Enter value for s_id: 2 Enter value for addr: 'sdf' Enter value for name: 'ewt' Enter value for demand: '20000' old 2: values(&s_id,&addr,&name,&demand) new 2: values(2,'sdf','ewt','20000')
1 row created.
STO_LOC
WORKERS
QUANTITY
----------------------------------- -------------------- ---------workshop fahg sdgg 200 300 400 5000ps 2000ps 100000
SQL> insert into supplier 2 values(&s_id,&addrs,&name,&demand); Enter value for s_id: 1 Enter value for addrs: 'dsf' Enter value for name: 'gdfh' Enter value for demand: '50000ps' old 2: values(&s_id,&addrs,&name,&demand) new 2: values(1,'dsf','gdfh','50000ps')
1 row created.
SQL> / Enter value for s_id: 2 Enter value for addrs: 'sdf' Enter value for name: 'ewt' Enter value for demand: 20000 old 2: values(&s_id,&addrs,&name,&demand) new 2: values(2,'sdf','ewt',20000)
1 row created.
SQL> / Enter value for s_id: 3 Enter value for addr: 'fhh' Enter value for name: 'poiu' Enter value for demand: 10000 old 2: values(&s_id,&addr,&name,&demand) new 2: values(3,'fhh','poiu',10000)
1 row created.
S_ID
ADDRS
2 ewt
sdf 20000
3 poiu
fhh 10000
Enter value for cash: 10000 Enter value for cheque: 3244 Enter value for e_pay: 'credit' old 2: values(&cash,&cheque,&e_pay) new 2: values(10000,3244,'credit')
1 row created.
SQL> / Enter value for cash: 300000 Enter value for cheque: 543654 Enter value for e_pay: 'debit' old 2: values(&cash,&cheque,&e_pay) new 2: values(300000,543654,'debit')
1 row created.
SQL> / Enter value for cash: 4354366 Enter value for cheque: 143246 Enter value for e_pay: 'shopping' old 2: values(&cash,&cheque,&e_pay) new 2: values(4354366,143246,'shopping')
1 row created.
CASH
CHEQUE
E_PAY
SQL> select dept_id from project 2 union 3 select dept_id from department;
DEPT_ID ---------1 2 3
SQL> select dept_id from project 2 union all 3 select dept_id from department;
DEPT_ID ---------1 2 3 1 2 3
6 rows selected.
----------------------------------------- -------- ---------------------------STO_LOC WORKERS QUANTITY NOT NULL VARCHAR2(35) VARCHAR2(20) VARCHAR2(10)
no rows selected
CAMERA
MOBILE
ORDER_ID
P_ID NAME
DEPT_ID
DELIVERY_DATE DELIVERY
ORDER_DATE ORDER_ID
Table altered.
DELIVERY
ORDER_DATE ORDER_ID
----------------------------------------- -------- ---------------------------DELIVERY ORDER_DATE ORDER_ID VARCHAR2(20) NUMBER(10) NOT NULL VARCHAR2(10)
Table altered.
Table altered.
Table altered.
Table altered.
----------------------------------------- -------- ---------------------------ORDER_ID ORDER_DATE DELIVERY NOT NULL VARCHAR2(10) VARCHAR2(10) VARCHAR2(10)
no rows selected
CAMERA
MOBILE
ORDER_ID
1q 2w 3e
SQL> / Enter value for order_id: 43 Enter value for order_date: 5 Enter value for delivery: 45 old 2: values(&order_id,&order_date,&delivery) new 2: values(43,5,45)
1 row created.
no rows selected
CAMERA
MOBILE
ORDER_ID
SQL>spool off CODE AGGR: SQL> create table department 2 ( 3 dept_id varchar(10)primary key, 4 name varchar(35), 5 addr varchar(45), 6* designation varchar(20)) 7 ;
Table created.
SQL> 1 create table projects 2 ( 3 p_id varchar(10), 4 name varchar(10), 5 dur varchar(10), 6 dept_id varchar(10), 7* foreign key (dept_id) references department(dept_id)) 8 ;
Table created.
----------------------------------------- -------- ---------------------------DEPT_ID NAME ADDR DESIGNATION NOT NULL VARCHAR2(10) VARCHAR2(35) VARCHAR2(45) VARCHAR2(20)
----------------------------------------- -------- ---------------------------P_ID NAME DUR DEPT_ID VARCHAR2(10) VARCHAR2(10) VARCHAR2(10) VARCHAR2(10)
Table altered.
----------------------------------------- -------- ---------------------------DEPT_ID NAME ADDR DESIGNATION NOT NULL VARCHAR2(10) VARCHAR2(35) VARCHAR2(45) VARCHAR2(20)
P_ID
VARCHAR2(10)
SQL> create table supplier 2 ( 3 s_id varchar(10)primary key, 4 addrs varchar(40), 5 name varchar(30), 6 demand varchar(10));
Table created.
----------------------------------------- -------- ---------------------------S_ID ADDRS NAME DEMAND NOT NULL VARCHAR2(10) VARCHAR2(40) VARCHAR2(30) VARCHAR2(10)
SQL> create table buy_order 2 ( 3 delivery_date number(10), 4 delivery varchar(20), 5 order_date number(10), 6* order_id varchar(10)primary key) SQL> /
Table created.
SQL> create table product 2 ( 3 camera varchar(10), 4 mobile varchar(10), 5 pc varchar(15), 6 order_id varchar(10), 7 foreign key (order_id) references buy_order(order_id) 8* ) SQL> /
Table created.
----------------------------------------- -------- ---------------------------DELIVERY_DATE DELIVERY ORDER_DATE ORDER_ID NUMBER(10) VARCHAR2(20) NUMBER(10) NOT NULL VARCHAR2(10)
Table created.
Table created.
SQL> insert into employee 2 values(&type); Enter value for type: 'permanent' old 2: values(&type)
new 2: values('permanent')
1 row created.
SQL> / Enter value for type: 'temporary' old 2: values(&type) new 2: values('temporary')
1 row created.
SQL> create table acc_section 2 ( 3 cash varchar(10), 4 cheque varchar(16), 5 e_pay varchar(10));
Table created.
SQL> insert into department 2* values(&dept_id,&name,&addr,&desigination,&p_id) SQL> / Enter value for dept_id: 1 Enter value for name: 'software' Enter value for addr: 'xyz' Enter value for desigination: 'asd' Enter value for p_id: 11 old 2: values(&dept_id,&name,&addr,&desigination,&p_id) new 2: values(1,'software','xyz','asd',11)
1 row created.
SQL> / Enter value for dept_id: 2 Enter value for name: 'hardware' Enter value for addr: 'abc' Enter value for desigination: 'dsg' Enter value for p_id: 22 old 2: values(&dept_id,&name,&addr,&desigination,&p_id) new 2: values(2,'hardware','abc','dsg',22)
1 row created.
Enter value for name: 'desk' Enter value for addr: 'pqr' Enter value for desigination: 'dfbv' Enter value for p_id: 33 old 2: values(&dept_id,&name,&addr,&desigination,&p_id) new 2: values(3,'desk','pqr','dfbv',33)
1 row created.
DEPT_ID
NAME
2 abc
hardware dsg 22
3 pqr
desk dfbv 33
SQL> insert into projects 2 values(&p_id,&name,&dur,&dept_id); Enter value for p_id: 11 Enter value for name: 'dfg' Enter value for dur: 365 Enter value for dept_id: 1
1 row created.
SQL> / Enter value for p_id: 22 Enter value for name: 'dgfg' Enter value for dur: 400 Enter value for dept_id: 2 old 2: values(&p_id,&name,&dur,&dept_id) new 2: values(22,'dgfg',400,2)
1 row created.
SQL> / Enter value for p_id: '33' Enter value for name: 'dsgsg' Enter value for dur: '500' Enter value for dept_id: '3' old 2: values(&p_id,&name,&dur,&dept_id) new 2: values('33','dsgsg','500','3')
1 row created.
P_ID
NAME
DUR
DEPT_ID
22 33
dgfg dsgsg
400 500
2 3
SQL> insert into buy_order 2 values(&delivery_date,&delivery,&order_date,&order_id); Enter value for delivery_date: 30 Enter value for delivery: 'mobile' Enter value for order_date: 12 Enter value for order_id: '1q' old 2: values(&delivery_date,&delivery,&order_date,&order_id) new 2: values(30,'mobile',12,'1q')
1 row created.
SQL> / Enter value for delivery_date: 21 Enter value for delivery: 'pc' Enter value for order_date: 31 Enter value for order_id: '2w' old 2: values(&delivery_date,&delivery,&order_date,&order_id) new 2: values(21,'pc',31,'2w')
1 row created.
SQL> / Enter value for delivery_date: 29 Enter value for delivery: 'camera' Enter value for order_date: 12
1 row created.
SQL> insert into product 2 values(&mobile,&camera,&order_id); Enter value for mobile: 's3' Enter value for camera: '10px' Enter value for order_id: '1q' old 2: values(&mobile,&camera,&order_id) new 2: values('s3','10px','1q')
1 row created.
SQL> / Enter value for mobile: 's3' Enter value for camera: '26px' Enter value for order_id: '2w' old 2: values(&mobile,&camera,&order_id) new 2: values('s3','26px','2w')
1 row created.
SQL> / Enter value for mobile: 's' Enter value for camera: '32px' Enter value for order_id: '3e' old 2: values(&mobile,&camera,&order_id)
new 2: values('s','32px','3e')
1 row created.
DELIVERY_DATE DELIVERY
ORDER_DATE ORDER_ID
CAMERA
MOBILE
ORDER_ID
no rows selected
SQL> insert into supplier 2 values(&s_id,&addr,&name,&demand); Enter value for s_id: 1 Enter value for addr: 'dsf' Enter value for name: 'asdd'
1 row created.
SQL> / Enter value for s_id: 2 Enter value for addr: 'sdf' Enter value for name: 'ewt' Enter value for demand: '20000' old 2: values(&s_id,&addr,&name,&demand) new 2: values(2,'sdf','ewt','20000')
1 row created.
STO_LOC
WORKERS
QUANTITY
----------------------------------- -------------------- ---------workshop fahg sdgg 200 300 400 5000ps 2000ps 100000
SQL> insert into supplier 2 values(&s_id,&addrs,&name,&demand); Enter value for s_id: 1 Enter value for addrs: 'dsf'
Enter value for name: 'gdfh' Enter value for demand: '50000ps' old 2: values(&s_id,&addrs,&name,&demand) new 2: values(1,'dsf','gdfh','50000ps')
1 row created.
SQL> / Enter value for s_id: 2 Enter value for addrs: 'sdf' Enter value for name: 'ewt' Enter value for demand: 20000 old 2: values(&s_id,&addrs,&name,&demand) new 2: values(2,'sdf','ewt',20000)
1 row created.
SQL> / Enter value for s_id: 3 Enter value for addr: 'fhh' Enter value for name: 'poiu' Enter value for demand: 10000 old 2: values(&s_id,&addr,&name,&demand) new 2: values(3,'fhh','poiu',10000)
1 row created.
S_ID
ADDRS
2 ewt
sdf 20000
3 poiu
fhh 10000
QL> insert into acc_section 2 values(&cash,&cheque,&e_pay); Enter value for cash: 10000 Enter value for cheque: 3244 Enter value for e_pay: 'credit' old 2: values(&cash,&cheque,&e_pay) new 2: values(10000,3244,'credit')
1 row created.
SQL> / Enter value for cash: 300000 Enter value for cheque: 543654 Enter value for e_pay: 'debit' old 2: values(&cash,&cheque,&e_pay) new 2: values(300000,543654,'debit')
1 row created.
SQL> / Enter value for cash: 4354366 Enter value for cheque: 143246 Enter value for e_pay: 'shopping' old 2: values(&cash,&cheque,&e_pay) new 2: values(4354366,143246,'shopping')
1 row created.
CASH
CHEQUE
E_PAY
---------- ---------------- ---------10000 300000 4354366 3244 543654 143246 credit debit shopping
STO_LOC
WORKERS
QUANTITY
----------------------------------- -------------------- ---------matunga andheri dadar 1459 84653 4306789 437605 75984375 5467856754
SUM(WORKERS) -----------4392901
AVG(WORKERS) -----------1464300.33
MIN(WORKERS) -------------------1459
MAX(WORKERS) -------------------84653
VARIANCE(WORKERS) ----------------6.0615E+12
COUNT(WORKERS) -------------3
STDDEV(WORKERS) --------------2462018.82
SQL>spool off