Oracle Project Final 1
Oracle Project Final 1
CREATE TABLE STATEMENT:-QUERY:- Create a table student_record with the following fields:Column name
Name
Class
Roll no
type
Varchar2
Varchar2
number
Size
20
15
4
Total marks
number
description
Name of the student
Class of the student
Roll no of the
student
Total marks of the
student
*the contents of the table can be viewed as:-SQL> select * from student;
NAME
CLASS
ROLL_NO TOTAL_MARKS PHONE_NO
-------------------- --------------- ---------- ----------- ---------Ram
bba
555
66
124356
Shyam
bba
444
99
1 row created
SQL> /
Enter value for 1: duplikate
Enter value for 2: bba2
Enter value for 3: 557
Enter value for 4: 89
Enter value for 5: 536475
Old 1: insert into student values ('&1','&2', &3, &4, &5)
New 1: insert into student values ('duplikate','bba2',557, 89,536475)
1 row created.
*the contents of the table can be viewed as:-SQL> select * from student;
NAME
CLASS
ROLL_NO TOTAL_MARKS PHONE_NO
-------------------- --------------- ---------- ----------- ---------Ram
bba
555
66
124356
Shyam
bba
444
99
Kate
bba2
556
98
14356
Duplikate
bba2
557
89
536475
======================================================
Updating records into table:SQL> update student set name='pkate' where roll no=444;
1 row updated.
SQL> select * from student;
NAME
CLASS
ROLL_NO TOTAL_MARKS PHONE_NO
-------------------- --------------- ---------- ----------- ---------Ram
bba
555
66
124356
Pkate
bba
444
99
Kate
bba2
556
98
14356
Duplikate
bba2
557
89
536475
SQL> update student set name='sam', phone_no=653487 where roll no=444;
1 row updated.
*the contents of the table can be viewed as:-SQL> select * from student;
NAME
CLASS
ROLL_NO
TOTAL_MARKS PHONE_NO
-------------------- --------------- ---------- ----------- ---------Ram
bba
555
66
124356
Sam
bba
444
99
653487
Kate
bba2
556
98
14356
Duplikate
bba2
557
89
536475
======================================================
10
Deleting records from a table:QUERY:- delete the record where name =ram..
SQL> delete from student where name='ram';
1 row deleted.
SQL> select * from student;
NAME
CLASS
ROLL_NO TOTAL_MARKS PHONE_NO
-------------------- --------------- ---------- ----------- ---------Sam
bba
444
99
653487
Kate
bba2
556
98
14356
Duplikate
bba2
557
89
536475
HISAL
1200
1400
2000
3000
9999
11
PHONE_NO
653487
14356
536475
1272583
12
NAME
CLASS
ROLL_NO TOTAL_MARKS
-------------------- --------------- ---------- ----------- ---------Sam
bba
444
99
Kate
bba2
556
98
Duplikate
bba2
557
89
Repeater
bba2
559
78
PHONE_NO
653487
14356
536475
1272583
13
14
15
Logical operators.
SQL> select name, total_marks, roll_no from student where class='bba2'
AND phone no=14356;
NAME
TOTAL_MARKS
-------------------- ----------- ---------Kate
98
ROLL_NO
556
16
NAME
CLASS
ROLL_NO TOTAL_MARKS PHONE_NO
-------------------- --------------- ---------- ----------- ---------Sam
bba
444
99
653487
Kate
bba2
556
98
14356
Duplikate
bba2
557
89
536475
Peter
bba2
559
78
1272583
SQL> select name, class from student where NOT (class='bba');
NAME
CLASS
-------------------- --------------Kate
bba2
Duplikate
bba2
Peter
bba2
Special operators
SQL> select name, class from student where total_marks
100;
NAME
CLASS
-------------------- --------------Sam
bba
Kate
bba2
between 90 and
17
in ('Kate, Sam');
NAME
CLASS
ROLL_NO TOTAL_MARKS PHONE_NO
-------------------- --------------- ---------- ----------- ---------Duplikate
bba2
557
89
536475
Peter
bba2
559
78
1272583
18
Working with null values:-SQL> insert into student (name, roll_no) values ('lavika', 578);
1 row created.
SQL> select * from student;
NAME
CLASS
ROLL_NO TOTAL_MARKS
-------------------- --------------- ---------- ----------- ---------Sam
bba
444
99
Kate
bba2
556
98
Duplikate
bba2
557
89
Peter
bba2
559
78
Diksha
bba
558
99
Smita
567
97
Lavika
578
99
7 rows selected.
SQL> select name from student where phone_no is null;
NAME
-------------------Smita
Lavika
PHONE_NO
653487
14356
536475
1272583
243564
19
20
query:SQL> select name, roll_no, total_marks from student order by roll_no asc,
total_marks desc;
NAME
ROLL_NO TOTAL_MARKS
-------------------- ---------- ----------Sam
444
99
Kate
556
98
Duplikate
557
89
Diksha
558
99
Peter
559
78
Smita
567
97
Lavika
578
99
7 rows selected.
======================================================
21
Pattern matching:query:SQL> select name, total_marks from student where name like '%Kate';
NAME
TOTAL_MARKS
-------------------- ----------Kate
98
Duplikate
89
query:SQL> select name, class from student where name like '_____';
NAME
CLASS
-------------------- --------------Peter
bba2
Smita
Column Concatenation:-query:SQL> select 'roll number of '||name|| ' is '||roll_no from student;
'ROLLNUMBEROF'||NAME||'IS'||ROLL_NO
------------------------------------------------------------------------------Roll number of Sam is 444
Roll number of Kate is 556
Roll number of duplikate is 557
Roll number of peter is 559
Roll number of diksha is 558
Roll number of smita is 567
Roll number of lavika is 578
7 rows selected.
22
23
ascii(string)
chr(x)
concat(x1,x2)
instr(string/column name,x)
length(x)
24
lpad(char1,n,[char2])
ltrim(string[,char(s)])
rpad(char1,n[,char2])
25
7 rows selected.
rtrim(string[,char(s)])
replace(<c1>,<c2>[,<c3>])
substr(z,x[,y])
26
initcap(string)
lower(string)
upper(string)
27
7 rows selected.
translate(char,find,new)
28
NUMERIC FUNCTIONS
abs(x)
ceil(x)
cos(x)
exp(X)
floor(x)
mod(x,y)
29
1
power(x,y)
round(x[,y])
sign(x)
sqrt(x)
trunc(x,n)
30
DATE FUNCTIONS
sysdate
add_months(date,n)
last_day(date)
months_between(date1,date2)
next_day(date,char)
31
GENERAL FUNCTIONS
Greatest(expr1[,expr2]...)
least(expr1[,expr2]...)
NVL(col,value)
UID
User
32
AGGREGATE FUNCTIONS
count(x)
sum(x)
avg(x)
33
min(x)
max(x)
QUERY:- To display the use of avg, sum, min and max together
SQL> select avg(total_marks), sum(total_marks), min(total_marks), max(total_marks) from student;
AVG(TOTAL_MARKS) SUM(TOTAL_MARKS) MIN(TOTAL_MARKS) MAX(TOTAL_MARKS)
---------------- ---------------- ---------------- ---------------94.1428571
659
78
99
GROUP BY CLAUSE
QUERY:- To list the sum of total_marks of each class in
student_record table
SQL> select class, sum(total_marks) from student group by class;
CLASS
SUM(TOTAL_MARKS)
--------------- ---------------bba
198
bba2
265
196
34
QUERY:- To list the sum of salary of each job and deptno in emp
table
SQL> select deptno, job, sum(sal) from emp group by deptno, job;
DEPTNO JOB
SUM(SAL)
---------- --------- ---------10 CLERK
1300
10 MANAGER
2450
10 PRESIDENT
5000
20 ANALYST
6000
20 CLERK
1900
20 MANAGER
2975
30 CLERK
950
30 MANAGER
2850
30 SALESMAN
5600
9 rows selected.
HAVING CLAUSE
QUERY:- To find the maximum salary of each department, but
show only the departments that have a maximum salary of more
than Rs.2000
SQL> select deptno,max(sal) from emp group by deptno having max(sal)>2000;
DEPTNO MAX(SAL)
---------- ---------10
5000
20
3000
30
2850
QUERY:- To the total salary, max and min salary and the average
salary of employee's jobwise, for deptno 20 and display only those
rows having average salary greater than 1000
SQL> select job, sum(sal), avg(sal), max(sal), min(sal) from emp
2 where deptno=20
3 group by job
4 having avg(sal)>1000;
JOB
SUM(SAL) AVG(SAL) MAX(SAL) MIN(SAL)
--------- ---------- ---------- ---------- ---------ANALYST
6000
3000
3000
3000
MANAGER
2975
2975
2975
2975
35
36
CARTESIAN JOIN:QUERY:- TO list the employee name from emp table and
department names from dept table
SQL> select name,dname from student,dept;
NAME
DNAME
-------------------- -------------sam
ACCOUNTING
kate
ACCOUNTING
duplikate
ACCOUNTING
peter
ACCOUNTING
diksha
ACCOUNTING
smita
ACCOUNTING
lavika
ACCOUNTING
sam
RESEARCH
kate
RESEARCH
duplikate
RESEARCH
peter
RESEARCH
NAME
DNAME
-------------------- -------------diksha
RESEARCH
smita
RESEARCH
lavika
RESEARCH
sam
SALES
kate
SALES
duplikate
SALES
peter
SALES
diksha
SALES
smita
SALES
lavika
SALES
sam
OPERATIONS
NAME
DNAME
-------------------- -------------kate
OPERATIONS
duplikate
OPERATIONS
peter
OPERATIONS
diksha
OPERATIONS
smita
OPERATIONS
lavika
OPERATIONS
28 rows selected.
37
38
SELF JOIN:QUERY:- TO list the names of the manager with the employee
record
SQL> select worker.ename "ename" ,manager.ename "manager"
2 from emp worker,emp manager
3 where worker.mgr=manager.empno;
ename
manager
---------- ---------SMITH
FORD
ALLEN
BLAKE
WARD
BLAKE
JONES
KING
MARTIN BLAKE
BLAKE
KING
CLARK
KING
SCOTT JONES
TURNER BLAKE
ADAMS
SCOTT
JAMES
BLAKE
ename
manager
---------- ---------FORD
JONES
MILLER CLARK
13 rows selected.
39
NESTED QUERIES
QUERY:- TO list the name of the employees who do the same job
as that of an employee number 7369
SQL> select ename,job from emp
2 where job=(select job from emp where empno=7369);
ENAME
JOB
---------- --------SMITH
CLERK
ADAMS
CLERK
JAMES
CLERK
MILLER CLERK
QUERY:- TO list the name and salary of the employee who gets
salary greater than the minimum salary in the employee table
SQL> SELECT ENAME,SAL FROM EMP WHERE SAL>(SELECT MIN(SAL) FROM EMP);
ENAME
SAL
---------- ---------ALLEN
1600
WARD
1250
JONES
2975
MARTIN
1250
BLAKE
2850
CLARK
2450
SCOTT
3000
KING
5000
TURNER
1500
ADAMS
1100
JAMES
950
ENAME
SAL
---------- ---------FORD
3000
MILLER
1300
13 rows selected.
40
41
OR
SQL> select ename,sal,deptno from emp
2 where sal IN(800,950,1300);
ENAME
SAL DEPTNO
---------- ---------- ---------SMITH
800
20
JAMES
950
30
MILLER
1300
10
QUERY:- TO list empno and name whose salary is greater than the
average salary of all the departments
SQL> select empno,ename,job from emp where sal>ALL (select avg(sal) from emp group by deptno);
EMPNO ENAME
JOB
---------- ---------- --------7566 JONES
MANAGER
7788 SCOTT ANALYST
7839 KING
PRESIDENT
7902 FORD
ANALYST
42
VIEWS
QUERY:- To create a view for the clerk
SQL> create view clerk as select * from emp where job='CLERK';
View created.
CLERK
CLERK
7788 23-MAY-87
7698 03-DEC-81
COMM
1100
950
COMM
43
SEQUENCES
QUERY:- Create a sequence 'empnumber' starting with value 100
and incremented by 2
SQL> create sequence empcode increment by 2 start with 100;
Sequence created.
44
45
PRIVILEGES
QUERY:- To grant all the permissions to the user 'peter'
SQL> create user peter IDENTIFIED BY repeater;
User created.
SQL> grant create session to peter;
Grant succeeded.
OR
SQL> grant connect,resource to peter;
Grant succeeded.
46
ROLES
QUERY:- To create a role
SQL> connect system/manager;
Connected.
SQL> create role myrole1 identified by myrole;
Role created.
47
48
49
50
Output:
loop executes 5 times
PL/SQL procedure successfully completed.
51
Output:
Enter value for n: 85
Old 5: n:=&n;
New 5: n:=85;
Grade is a
PL/SQL procedure successfully completed.
52
Output:
Enter value for n: 5
Old 4: n:=&n;
New 4: n:=5;
5
4
3
2
1
PL/SQL procedure successfully completed.
53
Output:
Enter value for n: 5
Old 4: n:=&n;
New 4: n:=5;
1
2
3
4
5
PL/SQL procedure successfully completed.
54
Output:
Enter value for n: 7
Old 4: n:=&n;
New 4: n:=7;
7
6
5
4
3
2
1
PL/SQL procedure successfully completed.
55
Output:
Enter value for n: 4
Old 4: n:=&n;
New 4: n:=4;
4* 1 = 4
4* 2 = 8
4* 3 = 12
4* 4 = 16
4* 5 = 20
4* 6 = 24
4* 7 = 28
4* 8 = 32
4* 9 = 36
4* 10 = 40
PL/SQL procedure successfully completed.
56
57
58
59
60
61
//EXCEPTION PROPAGATION....
SQL> set serveroutput on;
SQL> declare
2 salary number;
3 e number;
4 begin
5 e :=&e;
6 select sal into salary from emp where empno=e;
7 if salary>4000 then
8 raise greater_sal;
9 end if;
10 dbms_output.put_line(salary);
11 exception
12 when no_data_found then
13 dbms_output.put_line('empno doesn't exist');
14 end;
15 exception
16 when greater_sal then
17 dbms_output.put_line('salary is greater than 4000');
18 end;
19 /
Enter value for e: 123
old 5: e :=&e;
new 5: e :=123;
ERROR:
ORA-01756: quoted string not properly terminated