Vous êtes sur la page 1sur 5

Practical no: 6

Aim: Using SET operators, Date/Time Functions Group by clause (advance feature) and
advanced subqueries.

a. Using SET operators

b. Enhancement to the Group By Clause

c.Advanced subqueries.

1. Create a Table Borrower with fields Bname, Loan_no.


SQL> Create table Borrower (Name varchar2(10),Loan_no number(5));

-> Table created.

 USING INSERT statement


SQL> Insert into Borrower values ('A',10001);

-> 1 row Created.

SQL> Insert into Borrower values ('B',10002);

-> 1 row Created.

SQL> Insert into Borrower values ('C',10003);

-> 1 row Created.

Select all the values from Borrower table.


SQL> Select * from Borrower;

Name Loan_no
A 10001
B 10002
C 10003

Create table Depo with field Name and Loan no.

SQL> Create table Depo (Name varchar2(10),Loan_no number(5));

-> Table created.

SQL> insert into Depo values( B,10002);

->1 row Created.

SQL> insert into Depo values( C,10003);

->1 row Created.

SQL> insert into Depo values( D,10004);


->1 row Created.

SQL> select * from Depo;

Name Loan_no
B 10002
C 10003
D 10004

Using SET operators

SQL> select * from Borrower union select* from depo;

Name Loan_no
A 10001
B 10002
C 10003
D 10004

SQL> select * from Borrower intersect select* from depo;

Name Loan_no
B 10002
C 10003

SQL> select * from Borrower minus select* from depo;

Name Loan_no
A 10001

SQL> select * from depo minus select* from Borrower;

Name Loan_no
D 10004

SQL> Create table Emp 12(Eno number(5),Ename varchar2(10), Job varchar 2(10),mgrid
number (5), sal number(6), deptno number(5));

Table created.

SQL> Insert into Emp12 values(101,'A','HR',1003,10000,20);

1 row created.

SQL> Insert into Emp12 values(102,'B','Sales',1004,12000,30);


1 row created

SQL> Insert into Emp12 values(103,'C','Mgr',1008,20000,10);

1 row created

SQL> Insert into Emp12 values(104,'D','Mgr',1008,10000,10);

1 row created

SQL> Insert into Emp12 values(105,'E','Sales',1004,5000,30);

1 row created

SQL> Insert into Emp12 values(106,'F','Finance',1007,15000,40);

1 row created

SQL> Insert into Emp12 values(107,'G','Mgr',1008,25000,10);

1 row created

SQL> Insert into Emp12 values(108,'H','President',0,6000,10);

1 row created

SQL> select *from Emp12;

Eno Ename Job mgrid sal deptno

101 A HR 1003 10000 20

102 B Sales 1004 12000 30

103 C Mgr 1008 20000 10

104 D Mgr 1008 10000 10

105 E Sales 1004 5000 30

106 F Finance 1007 15000 40

107 G Mgr 1008 25000 10

108 H President 0 6000 10

Enhancement to the Group By Clause


SQL> select deptno,job,count(*),sum(sal) from Emp12 group by rollup(deptno,job);

see output.

SQL> select deptno,job,count(*),sum(sal) from Emp12 group by cube(deptno,job);

see output

SQL> Create table Dept1(deptno number(5),Eno number(5),deptname varchar2(10));


Table created.

SQL> Insert into Dept1 values(10,108,'mgmt');

1 row created.

SQL> Insert into Dept1 values(20,101,'HR');

1 row created.

SQL> Insert into Dept1 values(30,102,'Sales');

1 row created.

SQL> Insert into Dept1 values(40,105,'Sales');

1 row created.

SQL> Select * from Dept1;

deptno Eno deptname

10 108 mgmt

20 101 HR

30 102 Sales

40 105 Sales

Advanced subqueries.
SQL> select* from Emp12 where Eno in(Select Eno from Dept1 where deptname='Sales');

see output

SQL> update Emp12 set sal=sal +sal*0.01 where Eno in(Select Eno from Dept1 where
deptname='Sales');

2 rows updated.

SQL>select * from Emp12;

see output

SQL> delete from Emp12 where Eno in (select Eno from dept1 where deptno=30);

1 row deleted.

SQL> select * from Emp12;

see output

SQL> create table Dept2(deptno number(5),Eno number(5),deptname varchar2(10));

Table created.
SQL> insert into Dept2 select * from Dept1 where Eno in(Select Eno from Dept1);

4 rows created.

SQL> select * from Dept2;

see output

************************************************* END**********************************************

Vous aimerez peut-être aussi