Académique Documents
Professionnel Documents
Culture Documents
DESHMUKHI
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
Company database
Employee table creation:
create table employee(fname varchar2(10),lname varchar2(10),emp_id
varchar2(10),bdate varchar2(10),hdate varchar2(10),desgn varchar2(10),mgr_id
varchar2(10),salary number(10),dept_no varchar2(10));
Name
Null?
FNAME
LNAME
EMP_ID
BDATE
HDATE
DESGN
MGR_ID
SALARY
DEPT_NO
Type
VARCHAR2(10)
VARCHAR2(10)
VARCHAR2(10)
VARCHAR2(10)
VARCHAR2(10)
VARCHAR2(10)
VARCHAR2(10)
NUMBER(10)
VARCHAR2(10)
LNAME
g
t
n
m
t
m
d
EMP_ID
111
112
113
114
115
116
117
BDATE
23mar1959
17oct1989
17aug1989
22may1956
05nov1989
22may1956
02apr1989
HDATE
23mar2007
17oct2007
17aug2007
02jan2006
05nov2005
02may2005
02apr2006
DESGN
xyz1
xyz2
xyz3
xyz4
xyz5
xyz1
xyz3
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
desc department2;
Name
Null?
DEPT_NAME
DEPT_NO
Type
VARCHAR2(10)
VARCHAR2(10)
DEPT_NAME
HR
RESEARCH
MANAGEMENT
devpmt
recovery
DEPT_NO
11
12
13
14
15
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
Null?
EMP_ID
PROJ_NO
HRS
Type
NUMBER(5)
NUMBER(5)
NUMBER(5)
EMP_ID
PROJ_NO
111
114
116
117
HRS
101
104
105
104
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
10
11
12
8
desc project1;
Name
Null?
PROJ_NAME
PROJ_NO
PROJ_LOC
DEPT_NO
Type
VARCHAR2(30)
NUMBER(5)
VARCHAR2(30)
NUMBER(5)
PROJ_NO
PROJ_LOC
DEPT_NO
101 hyd
102 chennai
103 hyd
104 goa
105 banglore
106 jaipur
11
12
12
14
13
15
6 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
Name
Null?
DEPT_NO
DEPT_LOC
Type
NUMBER
VARCHAR2(30)
DEPT_NO
DEPT_LOC
11 hyderabad
12 chennai
12 hyderabad
13 bangalore
14 Goa
15 Jaipur
6 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
Bank database
Creating account table
create table accounts(acc_no number(20),b_name varchar2(20),balance
number(10));
desc accounts;
Name
Null?
ACC_NO
B_NAME
BALANCE
Type
NUMBER(20)
VARCHAR2(20)
NUMBER(10)
B_NAME
BALANCE
101 sb1
102 sb2
201 sb3
215 sb4
217 sb3
222 sb5
305 sb6
500
400
900
700
715
700
350
7 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
Null?
CUST_NAME
CUST_STREET
CUST_CITY
Type
VARCHAR2(20)
VARCHAR2(20)
VARCHAR2(20)
CUST_STREET
spring
senator
north
hill
main
alma
north
main
park
putnam
CUST_CITY
jaipur
hyderabad
chennai
delhi
calcutta
goa
chennai
calcutta
jaipur
stanford
10 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
Null?
Type
LOAN_NO
B_NAME
AMOUNT
NUMBER(20)
VARCHAR2(20)
NUMBER(20)
LOAN_NO
B_NAME
11 sb6
14 sb1
15 sb2
16 sb2
17 sb1
23 sb5
93 sb4
AMOUNT
900
1500
1500
1300
1000
2000
500
7 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
Null?
Type
VARCHAR2(20)
NUMBER(20)
CUST_NAME
LOAN_NO
anand
giri
shashank
sreekanth
kishore
karthik
karthik
babu
16
93
15
14
17
11
23
17
8 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
10
Name
Null?
CUST_NAME
ACC_NO
Type
VARCHAR2(20)
NUMBER(20)
ACC_NO
shashank
prided
pradeep
kishore
chanu
karthik
saleem
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
102
101
201
217
222
215
305
11
Null?
B_NAME
B_CITY
ASSETS
Type
VARCHAR2(20)
VARCHAR2(20)
NUMBER
B_NAME
sb3
sb1
sb4
sb7
sb2
sb8
sb5
sb6
B_CITY
hyderabad
hyderabad
bangalore
chennai
bangalore
mumbai
goa
bangalore
ASSETS
710000
900000
400000
370000
170000
30000
210000
80000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
12
Sailors_reserves_boats database
Creating reserves table
create table reserves(sid number(20),bid number(20),rdate varchar2(20));
desc reserves;
Name
Null?
SID
BID
RDATE
Type
NUMBER(20)
NUMBER(20)
VARCHAR2(20)
BID
1111
1112
1113
1114
1111
RDATE
101 10-may-09
102 23-mar-89
103 20-mar-06
104 23-mar-00
105 05-nov-89
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
13
SNAME
SRATING
1111 name1
1112 bob
1113 bhatt
1114 nano
SAGE
7
6
6
9
21
20
18
17
BCOLOR
101 green
102 white
103 black
104 cream
105 red
BNAME
vamsi
sudeep
giri
sree
krishna
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
14
LNAME
PRASHANTH
DEPT_NAME
M RESEARCH
DEPT_NO
101
101
103
LNAME
11 g
11 t
12 m
15
FNAME
LNAME
shashank
Anand
prashanth
g
m
m
LNAME
n
LNAME
t
n
m
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
16
COUNT(*)
AVG(SALARY)
2
1
2
11000
13000
66000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
17
14
12000
COUNT(*)
1
18
FNAME
shashank
nano
babu
Anand
sreekanth
prashanth
karthik
LNAME
g
t
n
m
t
m
d
7 rows selected.
SNAME
1111 name1
1112 bob
SRATING
SAGE
7
6
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
21
20
19
SID
1111
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
20
SNAME
name1
SNAME
bhatt
Bob
name1
nano
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
21
Ans:
SAGE
20
Ans:
SNAME
bhatt
Bob
name1
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
22
nano
SNAME
name1
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
23
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
24
CUST_NAME
LOAN_NO
karthik
sreekanth
shashank
anand
babu
kishore
karthik
Giri
11
14
15
16
17
17
23
93
8 rows
3. Find the names & loan_nos of all customers who have a loan at branch
SBI.
Ans:
select distinct b.loan_no, b.cust_name
from borrower b, loan l
where b.loan_no=l.loan_no and l.b_name='sb1';
LOAN_NO
CUST_NAME
14 sreekanth
17 babu
17 kishore
4. Find the names of all branches that have assets greater than at least one
branch located in bangalore
Ans:
select b_name from branch1 where assets>(select
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
25
7 rows selected.
5. Find the names of all customers where street address includes sub string
main
Ans
select distinct cust_name from customer where
cust_street like '%main%';
CUST_NAME
kishore
shashank
26
CUST_NAME
anand
giri
karthik
shashank
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
27
10 rows selected.
28
AVG(BALANCE)
807.5
AVG(BALANCE)
sb1
sb2
sb3
sb4
sb5
sb6
500
400
807.5
700
700
350
COUNT(D.CUST_NAME)
sb1
sb2
sb3
sb4
sb5
sb6
1
1
2
1
1
1
6 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
29
AGREEGATE FUNCTIONS
SQL>create table acc_mstr(acc_no varchar2(10),type
varchar2(10),operationmode varchar2(10), opendate varchar2(10),currbalance
number(10));
SQL>insert into acc_mstr values
('&acc_no','&type','&operationmode','&opendate',' &currbalance');
ACC_NO
B12771
H12902
W87532
L35193
TYPE
CA
SB
SB
CA
OPERATIONMODE
Single
Joint
Single
Single
OPENDATE
12-04-95
05-11-00
29-05-04
04-09-90
CURRBALANCE
2500
15000
65000
14500
30
Average Balance
24250
MINIMUM (min()):SQL>select min(currbalance)"Minimum Balance" from acc_mstr;
Minimum Balance
2500
MAXIMUM (max()):SQL>select max(currbalance)"Maximum Balance" from acc_mstr;
Maximum Balance
65000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
31
32
FNAME
d
sushma
vasa
kk
krishna
LNAME
raju
Venlakshmi
Devi
Kishore
Manohar
MNAME
Naga
Venkstlakshmi
Diwakar
Settee
Pantulu
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
33
Table created;
SQL>insert into emp_mstr values(&emp_no,&fname,&lname,&desg,
&branch_no);
EMP_NO
A111
B777
C666
FNAME
d
vasa
krishna
LNAME
raju
diwakar
manohar
DESG
clerk
manager
peon
BRANCH_NO
99254
65471
129873
EXIST clause:-
CUST_NO
B777
C666
MNAME
Venkatlakshmi
Diwakar
NOT EXIST clause:SQL> select cust_no,mname from cust_mstr E where not exists(select *from emp_mstr
where emp_no=E.cust_no);
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
34
CUST_NO
A111
D123
E321
MNAME
naga
setty
pantulu
ENAME
smith
aleen
jones
blake
Scott
King
JOB
clerk
salesman
manager
manager
analyst
president
SAL
800
1600
2975
2800
3000
5000
DEPT_NO
20
30
20
30
20
10
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
35
7900
7902
James
Ford
clerk
analyst
950
3000
20
10
JOB
president
analyst
DEPT_NO
10
10
JOB
salesman
manager
president
annalyst
DEPT_NO
30
30
10
10
JOB
clerk
manager
analyst
clerk
SAL
950
2975
3000
800
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
36
COUNT(DEPT_NO)
2
4
2
NAME
Venkatlakshmi
Diwakar
Rohan
Monty
SALARY
30000
35000
15000
25000
NAME
Sushma
Diwakar
SALARY
25000
25000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
37
103
104
Lakshman
Krishna
10000
15000
MAXIMUM SALARY:SQL>select max(salary)from(select salary from sal1 UNION select salary from sal2);
MAX(SALARY)
35000
MINIMUM SALARY:SQL>select min(salary)from(select salary from sal1 UNION select salary from sal2);
MIN(SALARY)
10000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
38
CUST_NO
A666
B222
C333
FNAME
aaa
bbb
ccc
LNAME
ravi
yyy
www
MNAME
Ooo
Ppp
Qqq
EMP_NO
A666
B222
C333
FNAME
madhav
bbb
ccc
LNAME
Venu
Yyy
www
DESG
clerk
manager
AsstMgr
BRANCH_NO
99254
65471
129873
39
ADD1
123-21
22/12-2
11/23-65
ADD2
PT colony
MAGISTIC
wst-street
CITY
Hyd
Bangalore
Vijayawada
STATE
AP
Karnataka
AP
PINCODE
500060
600006
500015
ACCT_FD_NO
CA612
CA335
SB112
FS771
CUST_NO
CA221
CA120
SB340
FS110
40
CUSYOMER/EMPLOYEE
Aaaravi
Madhavvenu
INTERSECT CLAUSE: SQL>select distinct cust_no from acc_fd_cust_detail where acct_fd_no like 'CA%'
OR acct_fd_no 'SB%'
INTERSECT
select distinct cust_no from acc_fd_cust_detail where acct_fd_no like 'CA%' OR
acct_fd_no 'FS%'
CUST_NO
CA120
MINUS: -
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
41
CUST_NO
CA120
Joins:
Explanation of joins with an example
Create table depositors(cdi number,cname varchar2(20),cage number)
Table created
Insert into depositors values(&cdi,&cname,&cage)
select * from depositors
CDI
501 c1
502 c2
503 c3
504 c4
CAME
CAGE
21
22
23
26
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
42
LNO
501
502
505
506
AMOUNT
101
102
105
106
2000
3000
4000
5000
Inner join:
Sql>select * from depositors d inner join borrowers b on d.cdi=b.cdi
CDI
CAME
501 c1
502 c2
CAGE
21
22
CDI
501
502
LNO
101
102
AMOUNT
2000
3000
Outer join:
Left outer join:
Sql>select * from depositors d left outer join borrowers b on d.cdi=b.cdi
CDI
CAME
501 c1
CAGE
21
CDI
501
LNO
101
AMOUNT
2000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
43
502 c2
504 c4
503 c3
22
26
23
502
102
3000
CAGE
21
22
CDI
501
502
505
506
LNO
101
102
105
106
AMOUNT
2000
3000
4000
5000
CAME
CAGE
c1
c2
c4
c3
21
22
26
23
CDI
501
502
LNO
101
102
AMOUNT
2000
3000
505
506
105
106
4000
5000
Cross join
Sql> select * from depositors cross join borrowers
CDI
CAME
501 c1
CAGE
21
CDI
501
LNO
101
AMOUNT
2000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
44
501
501
501
502
502
502
502
503
503
503
503
504
504
504
504
c1
c1
c1
c2
c2
c2
c2
c3
c3
c3
c3
c4
c4
c4
c4
21
21
21
22
22
22
22
23
23
23
23
26
26
26
26
502
505
506
501
502
505
506
501
502
505
506
501
502
505
506
102
105
106
101
102
105
106
101
102
105
106
101
102
105
106
3000
4000
5000
2000
3000
4000
5000
2000
3000
4000
5000
2000
3000
4000
5000
45
10/5
---------2
Numeric Functions:
These functions operate on Numeric data hence is the name.
Note: Argument num in the following functions is any float-valued number.
ABS(num): Returns absolute value of the given number.(i.e. Always positive value)
SQL> SELECT ABS(10) FROM DUAL;
ABS(10)
---------10
SQL> SELECT ABS(-10) FROM DUAL;
ABS(-10)
---------10
CEIL(num): It returns the smallest integer greater than the given number.
SQL> SELECT CEIL(123.456) FROM DUAL;
CEIL(123.456)
-------------
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
46
124
FLOOR(num): It returns the largest integer smaller than the given value.
SQL> SELECT FLOOR(123.456) FROM DUAL;
FLOOR(123.456)
-------------123
LN(num) : It returns natural logarithm value of num .
SQL> SELECT LN(10) FROM DUAL;
LN(10)
---------2.30258509
47
100
ROUND(m, n): It rounds the given float-valued number m to the n places after the decimal
SQL> SELECT ROUND(1.23456) FROM DUAL;
ROUND(1.23456)
-------------1
SQL> SELECT ROUND(1.23456,3) FROM DUAL;
ROUND(1.23456,3)
---------------1.235
SQRT(m): It calculates square root value of number m
SQL> SELECT SQRT(9) FROM DUAL;
SQRT(9)
---------3
TRUNC(m, n): It truncates given float-valued number m to n places after the decimal.
SQL> SELECT TRUNC(1.23456) FROM DUAL;
TRUNC(1.23456)
-------------1
SQL> SELECT TRUNC(1.23456,3) FROM DUAL;
TRUNC(1.23456,3)
---------------1.234
GREATEST(expr1, expr2, ) : It finds the greatest value among the given expressions.
SQL> SELECT GREATEST(4,7,3,5,9,2) FROM DUAL;
GREATEST(4,7,3,5,9,2)
--------------------9
LEAST(expr1, expr2, ): It finds the Lowest value among the given expressions.
SQL> SELECT LEAST(4,7,3,5,9,2) FROM DUAL;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
48
LEAST(4,7,3,5,9,2)
-----------------2
CHARACTER FUNCTIONS:
UPPER( str) : It converts all letters in the given
string str into Upper case.
SQL> SELECT UPPER('abcDEfg') FROM DUAL;
UPPER('
------ABCDEFG
LOWER(str): It converts all the letters in the given
string str into Lower Case.
SQL> SELECT LOWER('ABCDEfg') FROM
DUAL;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
49
LOWER('
------abcdefg
INITCAP(str): It converts first letter of every word
in the given string str into Upper Case and
remaining letters into lower case. It is like proper
function in FoxPro.
SQL> SELECT INITCAP('ABCDEF') FROM
DUAL;
INITCA
-----Abcdef
50
51
----------------------------------------------------27
INSTR(string, str, m, n): It displays nth occurrence
of str in the string string starting from m.
SQL> SELECT INSTR('TRYING TO KEEP THE
THINGS AS SIMPLE AS POSSIBLE','AS',1,2)
FROM DUAL
INSTR(
-----------------------37
Note : DUAL IS A TABLE WITH 1 COLUMN
AND 1 ROW OF DATA IN IT.
SQL > DESC DUAL
Name
Null? Type
----------------------------------------- -------- DUMMY
VARCHAR2(1)
SQL > SELECT * FROM DUAL;
DUMMY
-----------X
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
52
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
53
54
55
RTRIM('
------TONY BL
ASCII(): Displays equivalent ASCII value of a
character.
SQL > SELECT ASCII('A') FROM DUAL;
ASCII('A')
---------65
SQL >SELECT TRANSLATE('JOHN','H','N')
FROM DUAL;
TRAN
---JONN
OTHER FUNCTIONS:
Note: Arguments to the below given functions are in terms of radians
COS(x): It returns the Cosine of x.
SQL> SELECT COS(0) FROM DUAL;
COS(0)
---------Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
56
1
COSH(x) : It returns hyperbolic cosine of x.
SQL> SELECT COSH(0) FROM DUAL;
COSH(0)
---------1
SIN(x) : It returns sine of x.
SQL> SELECT SIN(0) FROM DUAL;
SIN(0)
---------0
SINH(x): It returns hyperbolic sine of x.
SQL> SELECT SINH(0) FROM DUAL;
SINH(0)
---------0
TAN(x): It returns tangent of x.
SQL> SELECT TAN(0) FROM DUAL;
TAN(0)
---------0
TANH(x): It returns hyperbolic tangent of x.
SQL> SELECT TANH(0) FROM DUAL;
TANH(0)
---------0
DATEFUNCTIONS:
ADD_MONTHS(date, n) : Adds n months to the specified date .
SQL> SELECT ADD_MONTHS('1-JAN-05',5) FROM DUAL;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
57
ADD_MONTHS
-----------------01-JUN-05
LAST_DAY(date): Gives last date of the specified month (date).
SQL> SELECT LAST_DAY('1-JAN-05') FROM DUAL;
LAST_DAY(
--------31-JAN-05
58
TO_DATE('
--------01-JAN-05
SQL> SELECT TO_DATE('MAR 05 01','MON YY DD') FROM DUAL;
TO_DATE('
--------01-MAR-05
SQL> SELECT TO_DATE('01/01/05', 'DD/MM/YY') FROM DUAL;
TO_DATE('
--------01-JAN-05
The USER, SYSDATE Functions:
USER function displays login name of the user.
SQL> SELECT USER FROM DUAL;
USER
-----------------------------SCOTT
SYSDATE function displays system date.(Date in your windows)
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
--------1-JAN-05
CONVERSION FUNCTIONS:
TO_CHAR: This function is used to convert a date or number to character string.
SQL> SELECT TO_CHAR(SYSDATE,'DAY DD MONTH YYYY') FROM DUAL
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
59
TO_CHAR (SYSDATE,'DAYDDMONTH
--------------------------SATURDAY 01 JANUARY 2005
SQL> SELECT TO_CHAR (SYSDATE,' DD DY MM YY') FROM DUAL;
TO_CHAR (SYSDA
------------01 SAT 01 05
NVL() Function: This function is used to substitute any null value with a user-defined value.
Consider the following data from EMP table of SCOTT.
SQL> SELECT EMPNO, ENAME, SAL, COMM FROM EMP;
EMPNO ENAME
SAL
COMM
---------- ---------- ---------- ------------------------7369 SMITH
800
7499 ALLEN
1600
300
7521 WARD
1250
500
7566 JONES
2975
7654 MARTIN
1250
1400
7698 BLAKE
2850
7782 CLARK
2450
7788 SCOTT
3000
7839 KING
5000
7844 TURNER
1500
0
7876 ADAMS
1100
7900 JAMES
950
7902 FORD
3000
7934 MILLER
1300
In the above table except for 7499, 7521, 7654 and 7844 all others commissions are null
To display their commission as 0 (zero)
60
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876
7900
7902
7934
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
800
1600
1250
2975
1250
2850
2450
3000
5000
1500
1100
950
3000
1300
100
300
500
100
1400
100
100
100
100
0
100
100
100
100
SQL> SELECT EMPNO, ENAME, SAL, NVL (COMM, 888) FROM EMP
EMPNO ENAME
SAL NVL(COMM,888)
---------- ---------- ---------- ------------7369 SMITH
800
888
7499 ALLEN
1600
300
7521 WARD
1250
500
7566 JONES
2975
888
7654 MARTIN
1250
1400
7698 BLAKE
2850
888
7782 CLARK
2450
888
7788 SCOTT
3000
888
7839 KING
5000
888
7844 TURNER
1500
0
7876 ADAMS
1100
888
7900 JAMES
950
888
7902 FORD
3000
888
7934 MILLER
1300
888
In above queries we have seen how to substitute a value when the comm is null.
If one want to display "Commission not payed" against the employees who have no
commission we can write the following query.
61
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
1250 500
2975 Commission Not Payed
1250 1400
2850 Commission Not Payed
2450 Commission Not Payed
3000 Commission Not Payed
5000 Commission Not Payed
1500 0
1100 Commission Not Payed
950 Commission Not Payed
3000 Commission Not Payed
1300 Commission Not Payed
SUBQUERIES
A query nested within a query is known as subquery.
For example, you want to see all the employees whose salary is above average
salary. For this you have to first compute the average salary using AVG function
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
62
and then compare employees salaries with this computed salary. This is possible
using subquery. Here
the sub query will first compute the average salary and then main query will
execute.
Select * from emp where sal > (select avg(sal) from emp);
Similarly we want to see the name and empno of that employee whose salary is
maximum.
Select * from emp where sal = (select max(sal) from emp);
To see second maximum salary
Select max(sal) from emp where
sal < (select max(sal) from emp);
Similarly to see the Third highest salary.
Select max(sal) from emp where
sal < (select max(sal) from emp Where
sal < (select max(sal) from emp));
We want to see how many employees are there whose salary is above average.
Select count(*) from emp where
sal > (select max(sal) from emp);
We want to see those employees who are working in Hyderabad. Remember emp
and dept are joined on deptno and city column is in the dept table. Assuming that
wherever the department is located the employee is working in that city.
Select * from emp where deptno
in (select deptno from dept where city=HYD);
You can also use subquery in FROM clause of SELECT statement.
For example the following query returns the top 5 salaries from employees table.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
63
Select sal from (select sal from emp order sal desc)
where rownum <= 5;
To see the sum salary deptwise you can give the following query.
Select sum(sal) from emp group by deptno;
Now to see the average total salary deptwise you can give a sub query in FROM
clause.
select avg(depttotal) from (select sum(sal) as depttotal from emp group by
deptno);
WITH
The above average total salary department wise can also be achieved in 9i using
WITH clause given below
WITH DEPTOT AS (select sum(sal) as dsal from emp
group by deptno)
select avg(dsal) from deptot;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
64
65
End;
OUTPUT:
1
2
3
4
5
6
7
8
9
10
66
while radius<=7
loop
area:=pi*power(radius,2);
insert into areas values(radius,area);
radius:=radius+1;
end loop;
end;
OUT PUT:SQL> select *from areas;
RADIUS
3
4
5
6
7
AREA
28.26
50.24
78.5
113.04
153.86
67
loop
n2:=n+n1;
dbms_output.put_line(n2);
n:=n1;
n1:=n2;
end loop;
End;
OUTPUT:fibbonacci series
0
1
1
2
3
5
8
13
21
68
n:=n+1;
end loop;
End;
OUTPUT:
1
3
5
7
9
11
69
dbms_output.put_line(n);
n:=n+1;
end loop;
end;
Output:old 5: n:=&n;
new 5: n:=30;
enter a number
display the numbers from 30 to 40:
30
31
32
33
34
35
36
37
38
39
40
6) Write a PL/SQL program to find the FACTORIAL OT THE
GIVEN NUMBER.
70
num:=#
temp:=num;
while num>0
loop
fact:=fact*num;
num:=num-1;
end loop;
insert into facto values(temp,fact);
end;
OUTPUT:procedure successfully completed
SQL>select *from facto;
NUM
5
FACT
120
71
while i<=n
loop
if n mod i=0
then
counts:=counts+1;
endif;
I:=I+1;
End loop;
If counts=2
Then
dbms_output.put_line(prime);
else
dbms_output.put_line(not prime);
end if;
end;
OUT PUT:old 3: n number(10):=&n;
new 3: n number(10):=6;
not prime
PL/SQL procedure successfully completed.
Function:8) Write a PL/SQL program to find the area of the circle with the
function
72
pi constant number(9,7):=3.14;
area number(14,2);
begin
area:=pi*power(radius,2);
return area;
end;
OUT PUT:-
73
return 'A';
WHEN score BETWEEN 65 AND 79 THEN
dbms_output.put_line('Between 65 and 79');
return 'B';
WHEN score BETWEEN 50 AND 64 THEN
dbms_output.put_line('Between 50 and 64');
return 'C';
WHEN score BETWEEN 40 AND 49 THEN
dbms_output.put_line('Between 40 and 49');
return 'D';
WHEN score BETWEEN 0 AND 39 THEN
dbms_output.put_line('Between 0 and 39');
return 'F';
ELSE
return 'Invalid score';
END CASE;
end Get_Grade1;
2.
create or replace function Get_Grade2(score IN NUMBER)
RETURN VARCHAR2
is
grade VARCHAR2(15);
begin
grade := CASE
WHEN score BETWEEN 80 AND 100 THEN 'A'
WHEN score BETWEEN 65 AND 79 THEN 'B'
WHEN score BETWEEN 50 AND 64 THEN 'C'
WHEN score BETWEEN 40 AND 49 THEN 'D'
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
74
Procedure:
create procedure area_c6(radius in number)
as
pi constant number(9,7):=3.14;
area number(14,2);
begin
area:=pi*power(radius,2);
insert into areas values(radius,area);
end;
EXCEPTION HANDLING:
1) WRITE A PL/SQL PROGRAM TO DEMONSTRATE THE
DIVIDE BY ZERO EXCEPTION
set serveroutput on
DECLARE
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
75
Num_a NUMBER := 6;
Num_b NUMBER;
BEGIN
Num_b := 0;
Num_a := Num_a / Num_b;
Num_b := 7;
dbms_output.put_line(' Value of Num_b ' ||
Num_b);
EXCEPTION
WHEN ZERO_DIVIDE
THEN
dbms_output.put_line('Trying to divide by zero');
dbms_output.put_line(' Value of Num_a ' || Num_a);
bms_output.put_line(' Value of Num_b ' || Num_b);
END;
OUT PUT:
76
EXCEPTION
WHEN INVALID_NUMBER
THEN
DBMS_OUTPUT.PUT_LINE (CONVERSION TO STRING
TO NUMBER FAILED);
END;
O/P:
CONVERSION TO STRING TO NUMBER FAILED
PL/SQL PROCEDURE SUCCESFULLY COMPLETED.
Triggers:A trigger is a pl/sql block structure which is fired when a DML statements
like Insert, Delete, Update is executed on a database table. A trigger is
triggered automatically when an associated DML statement is executed.
Syntax of Triggers
The Syntax for creating a trigger is:
77
78
79
(:old.product_id,
:old.product_name,
:old.supplier_name,
:old.unit_price);
END;
3) Lets update the price of a product.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
80
81
3) AFTER
82
End;
4) AFTER
83
Mesage
Current_Date
-----------------------------------------------------------Before update, statement level
26-Nov-2008
Before update, row level
26-Nov-2008
After update, Row level
26-Nov-2008
Before update, row level
26-Nov-2008
After update, Row level
26-Nov-2008
After update, statement level
26-Nov-2008
The above result shows 'before update' and 'after update' row level events
have occured twice, since two records were updated. But 'before update' and
'after update' statement level events are fired only once per sql statement.
The above rules apply similarly for INSERT and DELETE statements.
DESC USER_TRIGGERS;
NAME
Type
-------------------------------------------------------TRIGGER_NAME
VARCHAR2(30)
TRIGGER_TYPE
VARCHAR2(16)
TRIGGER_EVENT
VARCHAR2(75)
TABLE_OWNER
VARCHAR2(30)
BASE_OBJECT_TYPE
VARCHAR2(16)
TABLE_NAME
VARCHAR2(30)
COLUMN_NAME
VARCHAR2(4000)
REFERENCING_NAMES
VARCHAR2(128)
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
84
WHEN_CLAUSE
VARCHAR2(4000)
STATUS
VARCHAR2(8)
DESCRIPTION
VARCHAR2(4000)
ACTION_TYPE
VARCHAR2(11)
TRIGGER_BODY
LONG
This view stores information about header and body of the trigger.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II II)
85