0% found this document useful (0 votes)
38 views

Dbms Lab Manual SQL Command _021939

Uploaded by

Jhenkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Dbms Lab Manual SQL Command _021939

Uploaded by

Jhenkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

[Type here]

DBMS LAB MANUAL


1. Execute DDL Commands
Consider the table:
STUDENT (regno number, name varchar2, dob date, marks number)
a) Create the above table with suitable constraints.
b) Remove the existing attribute marks from the table.
c) Change the data type of regno from number to varchar2.
d) Add a new attribute phno to the existing table.
e) Insert 5 tuples into the table.
f) Display the tuples in table
2. Execute DML Commands
Consider the table:
LIBRARY(bid number, title varchar2, author varchar2, publisher varchar2,
yearof_pub number, price number)
a) create the above table.
b) Enter 5 tuples into the table.
c) Display all the tuples from the table.
d) Display different publishers from table.
e) Update price of all books with 5% GST amount.
f) Delete the details of book published by a specific Author.
g) Arrange the tuples in the alphabetical order of book title.
h) List the details of all books whose price range between 100 rs and 300
rs.
3. Execute DCL and group functions
Consider the table:
EMPLOYEE (EmpNo, EmpName, Dept, Salary, DOJ, Branch) Perform the
following operations:
a) Create the table
b) Insert 5 tuples into the table
c) Retrieve average salary of all employee
d) Retrieve number of employees
e) Retrieve distinct number of employee

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

f) Retrieve total salary of employee group by employee name and count


similar names
g) Display details of employees whose salary is greater than 50000.
h) Perform Commit and Rollback operation.

4. Implement the Nested Queries.


An INVENTORY database has the following table.
ITEMS(itemcode number, name varchar2, price number)
PURCHASE(itemcode number, qty number)
a) Create the tables with the above attributes.
b) Enter 5 tuples into the tables.
c) List the items purchased.
d) List the items which are not purchased by anyone.
5. Implement Join operations in SQL
The COMPANY database consists of the tables:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
Create tables, Insert 5 tuples each and perform the following
a) Give a 10 percent raise in salary for all employees working in the
‘Research’ Department.
b) Retrieve the name of each employee Controlled by department
number 5 (use EXISTS operator).
c) Retrieve the name of each dept and number of employees working in
each department which has at least
2 employees
d) Retrieve the name of employees and their department name (using
NATURAL JOIN)
e) Perform EQUI join operation on the given tables.
f) Perform NON-EQUI join operation on the given tables.
g) Perform OUTER join operations on the given tables
6. Create views for a particular table
The RAILWAY RESERVATION SYSTEM database consists of the tables:
TRAIN(TrainNo, TrainName, StartPlace, Destination)
AVAILABILITY(TrainNo, Class,StartPlace,Destination,No_of_seats)

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

a) Create view sleeper to display train no, start place, destination which
have sleeper class and perform the
following
• insert new record
• update destination=’Manglore’ where train no=’RJD16’
• delete a record which have train no=’KKE55’
b) Create view details to display train no, train name, class
c) Create view total_seats to display train number, start place, use count
function to no of seats , group by
start place and perform the following
• insert new record
• update start place=’Hubli’ where train no=’JNS8’
• delete last row of the view
d) Rename view sleeper to class
e) Delete view details
7. Write PL/SQL procedure to compute factorial of a number using
recursion
8. Given the table EMPLOYEE (EmpNo, Name, Salary, Designation,
DeptID) write a cursor in PL/SQL to
select the five highest paid employees from the table.
9. Given the table MOVIE(MID,MTitle, Language,Director,Year) write a
function in PL/SQL to find the
total number of Movies in the table.
10. Given the Table CUSTOMERS(CID,CName, Address) write a PL/SQL
program which asks for
customer ID, if the user enters invalid ID then the exception invalid_id
is raised

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

1. Execute DDL Commands


Consider the table:
STUDENT (regno number, name varchar2, dob date, marks number)
a) Create the above table with suitable constraints.
SQL> CREATE TABLE STUDENT(regno number(2),name varchar2(10),dob
date,marks number(2));
Table created.
SQL> desc STUDENT;
Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NUMBER(2)
NAME VARCHAR2(10)
DOB DATE
MARKS NUMBER(2)

b) Remove the existing attribute marks from the table


SQL> alter table student drop column marks;
Table altered.
SQL> desc student
Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NUMBER(2)
NAME VARCHAR2(10)
DOB DATE
c) Change the data type of regno from number to varchar2.
SQL> alter table student modify regno varchar2(10);
Table altered.
SQL> desc student
Name Null? Type

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

----------------------------------------- -------- ----------------------------


REGNO VARCHAR2(10)
NAME VARCHAR2(10)
DOB DATE
d) Add a new attribute phno to the existing table.
SQL> alter table student add phno number(10);
Table altered.
SQL> desc student
Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO VARCHAR2(10)
NAME VARCHAR2(10)
DOB DATE
PHNO NUMBER(10)
e) Insert 5 tuples into the table.
SQL> insert into student(regno,name,dob,phno)values('u11sc11','john','12jan2000’
,1234567890);

1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc22','ram','13jan2001'
,2234567890);

1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc33','sun','14jan2002'
,3234567890);

1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc44','moon','14feb2003'
,4234567890);

1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc55','ravi','02oct2004'
,5234567890);

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

1 row created.
f) Display the tuples in table
SQL> SELECT * FROM STUDENT;
REGNO NAME DOB PHNO
---------- ---------- --------- ----------
u11sc11 john 12-JAN-00 1234567890
u11sc22 ram 13-JAN-01 2234567890
u11sc33 sun 14-JAN-02 3234567890
u11sc44 moon 14-FEB-03 4234567890
u11sc55 ravi 02-OCT-04 5234567890

2. Execute DML Commands


Consider the table:
LIBRARY(bid number, title varchar2, author varchar2, publisher varchar2,
yearof_pub number, price number)
a) create the above table.
SQL> CREATE TABLE LIBRARY(bid number(10),title varchar(20),author
varchar(20),publisher varchar2(20),year_of_pub number(10),price
number(10));
Table created.
SQL> desc library;
Name Null? Type
----------------------------------------- -------- ----------------------------
BID NUMBER(10)
TITLE VARCHAR2(20)
AUTHOR VARCHAR2(20)
PUBLISHER VARCHAR2(20)
YEAR_OF_PUB NUMBER(10)
PRICE NUMBER(10)
b) Enter 5 tuples into the table.

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> insert into library values(10,'hamlet','willam shakespeare','oxford


university',2016,100);
1 row created.
SQL> insert into library values(11,'death of a salesman','arthur miller','penguin
classics',2011,200);
1 row created.
SQL> insert into library values(12,'the tempest','willam shakespeare','arden
shakespeare',2017,300);
1 row created.
SQL> insert into library values(14,'star trek chronology','okuda M','pocket
books',1993,500);
1 row created.
SQL> insert into library values(17,'waiting for godot','samvel beckett','grove
press',2011,600);
1 row created.
c) Display all the tuples from the table.
SQL> SELECT * FROM library;
BID TITLE AUTHOR PUBLISHER YEAR_OF_PUB PRICE
---------- -------------------- -------------------- -------------------- ------------ ---------
10 hamlet willamshakespeare oxford university 2016 100
11 death of a salesman arthur miller penguin classics 2011 200
12 the tempest willam shakespeare arden Shakespeare 2017 300
14 star trek chronology okuda M pocket books 1993 500
17 waiting for godot samvel beckett grove press 2011 600

d) Display different publishers from table.


SQL> SELECT PUBLISHER FROM LIBRARY;
PUBLISHER
oxford university
penguin classics
arden shakespeare

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

pocket books
grove press
e) Update price of all books with 5% GST amount.
SQL> UPDATE library SET price = price +(price*5/100);
5 rows updated.
SQL> SELECT * FROM library;

BID TITLE AUTHOR PUBLISHER YEAR_OF_PUB PRICE


---------- -------------------- -------------------- -------------------- ----------- ----------
10 hamlet willam shakespeare oxford university 2016 105
11 death of a salesman arthur miller penguin classics 2011 210
12 the tempest willam shakespeare arden Shakespeare 2017 315
14 star trek chronology okuda M pocket books 1993 525
17 waiting for godot samvel beckett grove press 2011 630
f) Delete the details of book published by a specific Author.
SQL> DELETE FROM library WHERE AUTHOR = 'okuda M';
1 row deleted.
SQL> SELECT * FROM library;
BID TITLE AUTHOR PUBLISHER YEAR_OF_PUB PRICE
---------- -------------------- -------------------- -------------------- ----------------------- --------
10 hamlet willam shakespeare oxford university 2016 105
11 death of a salesman arthur miller penguin classics 2011 210
12 the tempest willam shakespeare arden Shakespeare 2017 315
17 waiting for godot samvel beckett grove press 2011 630
g) Arrange the tuples in the alphabetical order of book title.
SQL> SELECT * FROM library ORDER BY title;

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

BID TITLE AUTHOR PUBLISHER YEAR_OF_PUB PRICE


---------- -------------------- -------------------- -------------------- ----------- ----------
11 death of a salesman arthur miller penguin classics 2011 210
10 hamlet willam shakespeare oxford university 2016 105
12 the tempest willam shakespeare arden Shakespeare 2017 315
17 waiting for godot samvel beckett grove press 2011 630
h) List the details of all books whose price range between 100 rs and 300 rs.
SQL> SELECT * FROM library WHERE price BETWEEN 100 AND 300;
BID TITLE AUTHOR PUBLISHER YEAR_OF_PUB PRICE
---------- -------------------- -------------------- -------------------- ----------- ----------
10 hamlet willam shakespeare oxford university 2016 105
11 death of a salesman arthur miller penguin classics 2011 210
3. Execute DCL and group functions
Consider the table:
EMPLOYEE (EmpNo, EmpName, Dept, Salary, DOJ, Branch) Perform the
following operations:
a) Create the table
SQL> CREATE TABLE employee(emp_no number primary key,emp_name
varchar2(10),dept varchar2(10),salary number(5),dob date,branch
varchar2(20));
Table created.
SQL> desc employee;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMP_NO NUMBER(5)
EMP_NAME VARCHAR2(10)
DEPT VARCHAR2(10)
SALARY NUMBER(5)

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

DOB DATE
BRANCH VARCHAR2(20)
b) Insert 5 tuples into the table
SQL> insert into employee values(10,'ravi','biotech',20000,'9-dec-2000','bsc');
1 row created.
SQL> insert into employee values(11,'ramesh','cs',26000,'3-jan-2003','bsc');
1 row created.
SQL> insert into employee values(13,'ramya','Acc',30000,'11-oct-2005','bcom');
1 row created.
SQL> insert into employee values(15,'rahul','cs',50000,'31-oct-2001','bca');
1 row created.
SQL> insert into employee values(14,'someshwar','biotech',55000,'9-dec-
2000','bsc');
1 row created.
SQL> SELECT * FROM employee;
EMP_NO EMP_NAME DEPT SALARY DOB BRANCH
---------- ---------- ---------- ---------- --------- --------------------
10 ravi biotech 20000 09-DEC-00 bsc
11 ramesh cs 26000 03-JAN-03 bsc
13 ramya Acc 30000 11-OCT-05 bcom
15 rahul cs 50000 31-OCT-01 bca
14 someshwar biotech 55000 09-DEC-00 bsc
c) Retrieve average salary of all employee
SQL> SELECT avg(salary)Avg_of_salary FROM employee;
AVG_OF_SALARY
-------------
36200
d) Retrieve number of employees

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> SELECT count(emp_no)No_of_employees FROM employee;


NO_OF_EMPLOYEE
5
e) Retrieve distinct number of employee
SQL> SELECT distinct emp_name FROM employee;
EMP_NAME
Ramesh
someshwar
rahul
ramya
ravi
f) Retrieve total salary of employee group by employee name and count
similar names
SQL> SELECT sum(salary),emp_name,count(emp_name) FROM employee
group by emp_name;
SUM(SALARY) EMP_NAME COUNT(EMP_NAME)
----------- ---------- ---------------
26000 ramesh 1
55000 someshwar 1
50000 rahul 1
30000 ramya 1
20000 ravi 1
g) Display details of employees whose salary is lessthan 50 000.
SQL> SELECT * FROM employee WHERE salary<50000;
EMP_NO EMP_NAME DEPT SALARY DOB BRANCH
---------- ---------- ---------- ---------- --------- --------------------
10 ravi biotech 20000 09-DEC-00 bsc
11 ramesh cs 26000 03-JAN-03 bsc

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

13 ramya Acc 30000 11-OCT-05 bcom


h) Perform Commit and Rollback operation.
COMMIT:
SQL> DELETE FROM Employee WHERE EMP_NO='11';
1 rows deleted.
SQL> commit;
Commit complete.
SQL> SELECT * FROM employee;
EMP_NO EMP_NAME DEPT SALARY DOB BRANCH
---------- ---------- ---------- ---------- --------- --------------------
10 ravi biotech 20000 09-DEC-00 bsc
13 ramya Acc 30000 11-OCT-05 bcom
15 rahul cs 50000 31-OCT-01 bca
14 someshwar biotech 55000 09-DEC-00 bsc
4 rows selected.
ROLLBACK:
SQL> DELETE FROM Employee WHERE EMP_NO='13';
1 rows deleted.
SQL> rollback;
Rollback complete.
SQL> SELECT * FROM employee;
EMP_NO EMP_NAME DEPT SALARY DOB BRANCH
---------- ---------- ---------- ---------- --------- --------------------
10 ravi biotech 20000 09-DEC-00 bsc
13 ramya Acc 30000 11-OCT-05 bcom
15 rahul cs 50000 31-OCT-01 bca
14 someshwar biotech 55000 09-DEC-00 bsc
4 rows selected.

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

4. Implement the Nested Queries.


An INVENTORY database has the following table.
ITEMS(itemcode number, name varchar2, price number
PURCHASE(itemcode number, qty number)
a) Create the tables with the above attributes.
SQL> CREATE TABLE items(item_code number(3),name varchar2(10),price
number(5));
Table created.
SQL> desc items;
Name Null? Type
----------------------------------------- -------- ----------------------------
ITEM_CODE NUMBER(3)
NAME VARCHAR2(10)
PRICE NUMBER(5)
SQL> CREATE TABLE purchase(item_code number(3),qty number(3));
Table created.
SQL> desc purchase;
Name Null? Type
----------------------------------------- -------- ----------------------------
ITEM_CODE NUMBER(3)
QTY NUMBER(3)
b) Enter 5 tuples into the tables.
SQL> insert into items values(120,'mouse',400);
1 row created.
SQL> insert into items values(198,'keyboard',800);
1 row created.

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> insert into items values(420,'monitor',1000);


1 row created.
SQL> insert into items values(528,'cpu',5000);
1 row created.
SQL> insert into items values(570,'router',500);
1 row created.
SQL> SELECT * FROM items;
ITEM_CODE NAME PRICE
---------- ---------- ----------
120 mouse 400
198 keyboard 800
420 monitor 1000
528 cpu 5000
570 router 500

SQL> insert into purchase values(120,50);


1 row created.
SQL> insert into purchase values(198,30);
1 row created.
SQL> insert into purchase values(420,5);
1 row created.
SQL> insert into purchase values(570,27);
1 row created.
SQL> SELECT * FROM purchase;
ITEM_CODE QTY
---------- ----------

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

120 50
198 30
420 5
570 27
c) List the items purchased.
SQL> SELECT * FROM items WHERE item_code in (select item_code from
purchase);
ITEM_CODE NAME PRICE
---------- ---------- -------
120 mouse 400
198 keyboard 800
420 monitor 1000
570 router 500
d) List the items which are not purchased by anyone.
SQL> SELECT * FROM items WHERE item_code not in (select item_code from
purchase);
ITEM_CODE NAME PRICE
---------- ---------- ----------
528 cpu 5000
5. Implement Join operations in SQL The COMPANY database consists of the
tables:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo) DEPARTMENT
(DNo, DName, MgrSSN, MgrStartDate)
Create tables, Insert 5 tuples each and perform the following
SQL> CREATE TABLE employee(ssn number(10),Name varchar2(10),Address
varchar2(10),Sex varchar2(7),Salary number(5),SuperSSN number(10),DNo
number(5));
Table created.

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> desc employee;


Name Null? Type
----------------------------------------- -------- ----------------------------
SSN NUMBER(10)
NAME VARCHAR2(10)
ADDRESS VARCHAR2(10)
SEX VARCHAR2(7)
SALARY NUMBER(5)
SUPERSSN NUMBER(10)
DNO NUMBER(5)
SQL> CREATE TABLE DEPARTMENT (DNo number(5),DName
varchar2(15),MgrSSN number(10),MgrStartDate date);
Table created.
SQL> desc department;
Name Null? Type
----------------------------------------- -------- ----------------------------
DNO NUMBER(5)
DNAME VARCHAR2(15)
MGRSSN NUMBER(10)
MGRSTARTDATE DATE
SQL> insert into employee values ('&ssn','&Name','&Address', '&sex','&salary',
'&superSSN','&DNo');
Enter value for ssn: 5432
Enter value for name: vijay
Enter value for address: tumkur
Enter value for sex: male
Enter value for salary: 60000

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

Enter value for superssn: 780


Enter value for dno: 1
1 row created.
SQL> /
Enter value for ssn: 5462
Enter value for name: geetha
Enter value for address: banglore
Enter value for sex: female
Enter value for salary: 37000
Enter value for superssn: 797
Enter value for dno: 2
1 row created.
SQL> /
Enter value for ssn: 6852
Enter value for name: suhas
Enter value for address: hassan
Enter value for sex: male
Enter value for salary: 25000
Enter value for superssn: 970
Enter value for dno: 3
1 row created.
SQL> /
Enter value for ssn: 5490
Enter value for name: rahul
Enter value for address: arsikere
Enter value for sex: male

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

Enter value for salary: 80000


Enter value for superssn: 148
Enter value for dno: 4
1 row created.
SQL> /
Enter value for ssn: 1436
Enter value for name: shalini
Enter value for address: tumkur
Enter value for sex: female
Enter value for salary: 50000
Enter value for superssn: 580
Enter value for dno: 5
1 row created.
SQL> select * from employee;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO
---------- ---------- ---------- ------- ---------- ---------- ----------
5432 vijay tumkur male 60000 780 1
5462 geetha banglore female 37000 797 2
6852 suhas hassan male 25000 970 3
5490 rahul arsikere male 80000 148 4
1436 shalini tumkur female 50000 580 5
SQL> insert into department values ('&DNo','&DName', '&MgrSSN',
'&MgrstartDate');
Enter value for dno: 1
Enter value for dname: research
Enter value for mgrssn: 750
Enter value for mgrstartdate: 01-jan-2012

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

1 row created.
SQL> /
Enter value for dno: 2
Enter value for dname: research
Enter value for mgrssn: 850
Enter value for mgrstartdate: 02-mar-2009
1 row created.
SQL> /
Enter value for dno: 3
Enter value for dname: cs
Enter value for mgrssn: 950
Enter value for mgrstartdate: 05-feb-2015
1 row created.
SQL> /
Enter value for dno: 4
Enter value for dname: bio-tech
Enter value for mgrssn: 650
Enter value for mgrstartdate: 02-dec-2018
1 row created.
SQL> /
Enter value for dno: 6
Enter value for dname: cs
Enter value for mgrssn: 550
Enter value for mgrstartdate: 08-jun-2023
1 row created.
SQL> select * from department;

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

DNO DNAME MGRSSN MGRSTARTD


---------- ------------- ---------- ---------
1 research 750 01-JAN-12
2 research 850 02-MAR-09
3 cs 950 05-FEB-15
4 bio-tech 650 02-DEC-18
6 cs 550 08-JUN-23
a).Give a 10 percent raise in salary for all employees working in the ‘Research’
Department.
SQL> UPDATE employee SET salary=salary+(salary*10/100)WHERE Dno in
(SELECT Dno FROM department WHERE Dname='research');
2 rows updated.
SQL> select * from employee;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO
---------- ---------- ---------- ------- ---------- ---------- ----------
5432 vijay tumkur male 66000 780 1
5462 geetha banglore female 40700 797 2
6852 suhas hassan male 25000 970 3
5490 rahul arsikere male 80000 148 4
1436 shalini tumkur female 50000 580 5
b).Retrieve the name of each employee Controlled by department number 5
(use EXISTS operator).
SQL> SELECT DName FROM department WHERE EXISTS (SELECT DName FROM
employee WHERE employee.DNo = department.DNo AND DNo='5');
no rows selected
c).Retrieve the name of each dept and number of employees working in each
department which has at least 2 employees

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> select DName,count(DName)No_of_employees from department group


by DName;
DNAME NO_OF_EMPLOYEES
--------------- ---------------
cs 2
bio-tech 1
research 2
d).Retrieve the name of employees and their department name (using
NATURAL JOIN)
SQL> select name,DName from employee NATURAL JOIN department;
NAME DNAME
---------- ----------
vijay research
geetha research
suhas cs
rahul bio-tech
e).Perform EQUI join operation on the given tables.
SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e,department d where e.DNo = d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD
---------- ---------- --------- ------- ---------- ---------- ---------- --------------- ---------- ---------
5432 vijay tumkur male 66000 780 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 2 research 850 02-MAR-09
5490 suhas hassan male 25000 970 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 4 bio-tech 650 02-DEC-18
f).Perform NON-EQUI join operation on the given tables.

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e,department d where e.DNo <> d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD
---------- ---------- --------- ------- ---------- ---------- ---------- --------------- ---------- ---------

5432 vijay tumkur male 66000 780 2 research 850 02-MAR-09


5432 vijay tumkur male 66000 780 3 cs 950 05-FEB-15
5432 vijay tumkur male 66000 780 4 bio-tech 650 02-DEC-18
5432 vijay tumkur male 66000 780 6 cs 550 08-JUN-23
5462 geetha banglore female 40700 797 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 3 cs 950 05-FEB-15
5462 geetha banglore female 40700 797 4 bio-tech 650 02-DEC-18
5462 geetha banglore female 40700 797 6 cs 550 08-JUN-23
6852 suhas hassan male 25000 970 1 research 750 01-JAN-12
6852 suhas hassan male 25000 970 2 research 850 02-MAR-09
6852 suhas hassan male 25000 970 4 bio-tech 650 02-DEC-18
6852 suhas hassan male 25000 970 6 cs 550 08-JUN-23
5490 rahul arsikere male 80000 148 1 research 750 01-JAN-12
5490 rahul arsikere male 80000 148 2 research 850 02-MAR-09
5490 rahul arsikere male 80000 148 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 6 cs 550 08-JUN-23
1436 shalini tumkur female 50000 580 1 research 750 01-JAN-12
1436 shalini tumkur female 50000 580 2 research 850 02-MAR-09
1436 shalini tumkur female 50000 580 3 cs 950 05-FEB-15
1436 shalini tumkur female 50000 580 4 bio-tech 650 02-DEC-18
1436 shalini tumkur female 50000 580 6 cs 550 08-JUN-23
21 rows selected.

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

g).Perform OUTER join operations on the given table


LEFT JOIN :-
SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e LEFT JOIN department d ON e.DNo = d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD

---------- ---------- ---------- ------- ---------- ---------- --- ------- ---------- ---------
5432 vijay tumkur male 66000 780 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 2 research 850 02-MAR-09
6852 suhas hassan male 25000 970 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 4 bio-tech 650 02-DEC-18
1436 shalini tumkur female 50000 580 null null null null
RIGHT JOIN :-
SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e RIGHT JOIN department d ON e.DNo =
d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD

---------- ---------- ---------- ------- ---------- ---------- --- ------- ---------- ---------
5432 vijay tumkur male 66000 780 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 2 research 850 02-MAR-09
6852 suhas hassan male 25000 970 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 4 bio-tech 650 02-DEC-18
null null null null null null 6 cs 550 08-JUN-23
FULL JOIN :-
SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e FULL JOIN department d ON e.DNo = d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

---------- ---------- ---------- ------- ---------- ---------- --- ------- ---------- ---------
5432 vijay tumkur male 66000 780 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 2 research 850 02-MAR-09
6852 suhas hassan male 25000 970 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 4 bio-tech 650 02-DEC-18
null null null null null null 6 cs 550 08-JUN-23
1436 shalini tumkur female 50000 580 null null null null
4 rows selected.
6. Create views for a particular table
The RAILWAY RESERVATION SYSTEM database consists of the tables:
TRAIN(TrainNo, TrainName, StartPlace, Destination)
AVAILABILITY(TrainNo, Class,StartPlace,Destination,No_of_seats)
SQL> create table train(trainno varchar2(7),train_name varchar2(25),startplace
varchar2(25),destination varchar2(25));
Table created.
SQL> desc train;
Name Null? Type
----------------------------------------- -------- ----------------------------
TRAINNO VARCHAR2(7)
TRAIN_NAME VARCHAR2(25)
STARTPLACE VARCHAR2(25)
DESTINATION VARCHAR2(25)
SQL> create table availability(trainno varchar2(7),class varchar2(10),startplace
varchar2(25),destination varchar2(25),no_of_seats number(5));
Table created.
SQL> desc availability;
Name Null? Type

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

----------------------------------------- -------- ----------------------------


TRAINNO VARCHAR2(7)
CLASS VARCHAR2(10)
STARTPLACE VARCHAR2(25)
DESTINATION VARCHAR2(25)
NO_OF_SEATS NUMBER(5)

SQL> insert into train values('RJD16','vishvamanava','hassan','sakleshpura');


1 row created.
SQL> insert into train values('KKE55','DEMU express','tumkur','arsikere');
1 row created.
SQL> insert into train values('JNS8','vasco','banglore','Goa');
1 row created.
SQL> insert into train values('DS78','dadar express','ksr','dadar');
1 row created.
SQL> insert into train values('DV99','talguppa express','ksr','Talguppa');
1 row created.

SQL> select * from train;


TRAINNO TRAIN_NAME STARTPLACE DESTINATION
----------- ------------------------- -------------------- -------------------------
RJD16 vishvamanava hassan sakleshpura
KKE55 DEMU express tumkur arsikere
JNS8 vasco banglore Goa
DS78 dadar express ksr dadar
DV99 talguppa express ksr Talguppa

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> insert into availability values('RJD16','sleeper','hassan','sakleshpura',15);


1 row created.
SQL> insert into availability values('KKE55','II class','tumkur','arsikere',22);
1 row created.
SQL> insert into availability values('JNS8','I class','banglore','Goa',15);
1 row created.
SQL> insert into availability values('DS78','AC','ksr','dadar',8);
1 row created.
SQL> insert into availability values('DV99','sleeper','ksr','Talguppa',18);
1 row created.

SQL> select * from availability;


TRAINNO CLASS STARTPLACE DESTINATION NO_OF_SEATS
---------- ---------- -------------------- ------------------------- -----------
RJD16 sleeper hassan sakleshpura 15
KKE55 II class tumkur arsikere 22
JNS8 I class banglore Goa 15
DS78 AC ksr dadar 8
DV99 sleeper ksr Talguppa 18
a).Create view sleeper to display train no, start place, destination which have
sleeper class and perform the following
SQL> create view sleeper as select trainno,startplace,destination from
availability where class='sleeper';
View created.
SQL> select *from sleeper;
TRAINNO STARTPLACE DESTINATION
----------- ------------------------- -------------------------

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

RJD16 hassan sakleshpura


DV99 ksr Talguppa

• insert new record


SQL> insert into train values('MNO20','mangala express','manglore','chennai');
1 row created.
• update destination=’Manglore’ where train no=’RJD16’
SQL> update train set destination='manglore' where trainno='RJD16';
1 row updated.
SQL> SELECT * FROM train;
TRAINNO TRAIN_NAME STARTPLACE DESTINATION
---------- ------------------------- ----------------- -------------------------
RJD16 vishvamanava hassan manglore
KKE55 DEMU express tumkur arsikere
JNS8 vasco banglore Goa
DS78 dadar express ksr dadar
DV99 talguppa express ksr Talguppa
MNO20 mangala express manglore Chennai
6 rows selected.
• delete a record which have train no=’KKE55’
SQL> delete from train where trainno='KKE55';
1 row deleted.
SQL> SELECT * FROM train;
TRAINNO TRAIN_NAME STARTPLACE DESTINATION
---------- ------------------------- ------------------------- -------------------------
RJD16 vishvamanava hassan sakleshpura

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

NS8 vasco banglore Goa


DS78 dadar express ksr dadar
DV99 talguppa express ksr Talguppa
MNO20 mangala express manglore Chennai
b) Create view details to display train no, train name, class
SQL> create view TRAINDETAILS as select t.trainno,t.train_name,a.class from
availability a,train t where t.trainno=a.trainno;
View created.
SQL> select * from traindetails ;
TRAINNO TRAIN_NAME CLASS
--------- ------------------------- ----------
RJD16 vishwamanava sleeper
JNS8 vasco I class
DS78 dadar express AC
DV99 talguppa express sleeper
c) Create view total_seats to display train number, start place, use count
function to no of seats , group by start place and perform the following
SQL> create view Totalseats as select t.trainno,a.startplace,no_of_seats from
availability a,train t where t.trainno=a.trainno;
View created.
SQL> select *from totalseats;
TRAINNO STARTPLACE NO_OF_SEATS
------------ ------------------- -----------
RJD16 hassan 15
JNS8 hubli 15
DS78 ksr 8
DV99 ksr 18

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

• insert new record


SQL> insert into train values('ABC19','Rani channamma','delhi','Banglore');
1 row created.
• update start place=’Hubli’ where train no=’JNS8’
update availability set startplace='hubli' where trainno='JNS8';
1 row updated.
SQL> select * from availability;
TRAINNO CLASS STARTPLACE DESTINATION NO_OF_SEATS
---------- ---------- -------------------- ------------------------- -----------
RJD16 sleeper hassan sakleshpura 15
KKE55 II class tumkur arsikere 22
JNS8 I class hubli Goa 15
DS78 AC ksr dadar 8
DV99 sleeper ksr Talguppa 18
• delete last row of the view
SQL> Delete from availability where trainno=(select max(trainno)from
totalseats);
1 row deleted.
d) Rename view sleeper to class
SQL> Rename sleeper to class;
Renamed.
e) Delete view details
SQL> drop view traindetails;
View dropped.

7. Write PL/SQL procedure to compute factorial of a number using recursion

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

SQL> declare
2 num number;
3 factorial number;
4 function calculateFact(x number)
5 return number
6 is
7 f number;
8 begin
9 if x=0 then
10 f:=1;
11 else
12 f:=x*calculateFact(x-1);
13 end if;
14 return f;
15 end;
16 begin
17 num:=2;
18 factorial:=calculateFact(num);
19 DBMS_OUTPUT.PUT_LINE('factorial'||num||'is'||factorial);
20 end;
21 /
factorial2is2
PL/SQL procedure successfully completed.
8. Given the table EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID)
write a cursor in PL/SQL to
select the five highest paid employees from the table.
SQL> CREATE table employee12(empno number(6),name varchar2(20),salary
number(10),designation varchar2(20),deptID number(10));

Table created.

SQL> insert into employee12


values('&empno','&name','&salary','&designation','&deptID');
Enter value for empno: 101
Enter value for name: Archana
Enter value for salary: 20000
Enter value for designation: Accountant

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

Enter value for deptid: 111


1 row created.

SQL> /
Enter value for empno: 102
Enter value for name: lekhana
Enter value for salary: 10000
Enter value for designation: manager
Enter value for deptid: 112
1 row created.

SQL> /
Enter value for empno: 103
Enter value for name: kavya
Enter value for salary: 30000
Enter value for designation: teacher
Enter value for deptid: 113
1 row created.

SQL> /
Enter value for empno: 104
Enter value for name: shree
Enter value for salary: 25000
Enter value for designation: lecturer
Enter value for deptid: 114
row created.

SQL> /
Enter value for empno: 105
Enter value for name: latha
Enter value for salary: 15000
Enter value for designation: hr mnager
Enter value for deptid: 115
1 row created.

SQL> /
Enter value for empno: 106

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

Enter value for name: laxmi


Enter value for salary: 22000
Enter value for designation: bank manager
Enter value for deptid: 116
old 1: insert into employee12
1 row created.

SQL> /
Enter value for empno: 107
Enter value for name: rachna
Enter value for salary: 33000
Enter value for designation: engineer
Enter value for deptid: 117
1 row created.

SQL> /
Enter value for empno: 108
Enter value for name: sinchana
Enter value for salary: 12000
Enter value for designation: owner
Enter value for deptid: 118
1 row created.

SQL> /
Enter value for empno: 109
Enter value for name: jaya
Enter value for salary: 23000
Enter value for designation: home worker
Enter value for deptid: 119
1 row created.

SQL> /
Enter value for empno: 110
Enter value for name: teju
Enter value for salary: 22000
Enter value for designation: professor
Enter value for deptid: 120

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

1 row created.

SQL> select * from employee12;


EMPNO NAME SALARY DESIGNATION DEPTID
---------- -------------------- ---------- -------------------- ----------
101 Archana 20000 Accountant 111
102 lekhana 10000 manager 112
103 kavya 30000 teacher 113
104 shree 25000 lecturer 114
105 latha 15000 hr mnager 115
106 laxmi 22000 bank manager 116
107 rachna 33000 engineer 117
108 sinchana 12000 owner 118
109 jaya 23000 home worker 119
110 teju 22000 professor 120

10 rows selected.

SQL> set serveroutput on


SQL> declare
2 cursor emp is
3 select empno,salary from employee12 order by salary desc;
4 begin
5 dbms_output.put_line('empno'||' '||'salary');
6 dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
7 for i in emp
8 loop
9 if emp % rowcount<=5 then
10 dbms_output.put_line(i.empno||' '||i.salary);
11 end if;
12 end loop;
13 end;
14 /
OUT PUT:
empno salary
~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~
107 33000

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

103 30000
104 25000
109 23000
110 22000
PL/SQL procedure successfully completed.
9. Given the table MOVIE(MID,MTitle, Language,Director,Year) write a
function in PL/SQL to find the
total number of Movies in the table.
SQL> create table movie(mid varchar2(10) primary key,mtitle varchar2(20) not
null, language varchar2(15) not null,director varchar2(25) not null, year
number(4) not null);
Table created.
SQL> desc movie;
Name Null? Type
----------------------------------------- -------- ----------------------------
MID NOT NULL VARCHAR2(10)
MTITLE NOT NULL VARCHAR2(20)
LANGUAGE NOT NULL VARCHAR2(15)
DIRECTOR NOT NULL VARCHAR2(25)
YEAR NOT NULL NUMBER(4)
SQL> insert into movie4 values ('&mid','&mtitle',' &language',' &director','
&year');
Enter value for mid: m123
Enter value for mtitle: taare
Enter value forlanguage: hindi
Enter value for director: kapoor
Enter value for year: 2000
1 row created.
SQL> /

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

Enter value for mid: m342


Enter value for mtitle: kranti
Enter value forlanguage: kannada
Enter value for director: yogarajbhat
Enter value for year: 2023
1 row created.
SQL> /
Enter value for mid: m126
Enter value for mtitle: kgf
Enter value forlanguage: kannada
Enter value for director: rakshithshetty
Enter value for year: 2020
1 row created.
SQL> /
Enter value for mid: m129
Enter value for mtitle: kantara
Enter value forlanguage: kannada
Enter value for director: sudeep
Enter value for year: 2000
1 row created.
SQL> /
Enter value for mid: m421
Enter value for mtitle: rajkumara
Enter value forlanguage: kannada
Enter value for director: pavan
Enter value for year: 2021

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

1 row created.
SQL> select*from movie4;
MID MTITLE LANGUAGE DIRECTOR YEAR
---------- -------------------- --------------- ------------------------- ----------
m123 taare hindi Kapoor 2000
m342 kranti kannada yogarajbhat 2023
m126 kgf kannada rakshithshetty 2020
m129 kantara kannada sudeep 2000
m421 rajkumara kannada pavan 2021

SQL> set serveroutput on


SQL> declare
2 cursor mov is select*from movie;
3 n int;
4 begin
5 n:=0;
6 forrowin mov loop
7 n:=n+1;
8 end loop;
9 dbms_output.put_line('total number of movies is'||n);
10 end;
11 /
total number of movies is5
PL/SQLprocedure successfully completed
10. Given the Table CUSTOMERS(CID,CName, Address) write a PL/SQL
program which asks for

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

customer ID, if the user enters invalid ID then the exception invalid_id is
raised
SQL>create table customer(CID number(10),CName varchar2(20),Address
varchar2(20));
Table created.
SQL> desc customer;
Name Null? Type
---------------------------------------- -------- ----------------------------
CID NUMBER(10)
CNAME VARCHAR2(20)
ADDRESS VARCHAR2(20)
SQL> insert into customer values(12,'Arjun','Tiptur');
1 row created.
SQL> insert into customer values(13,'Aryan','Tumkur');
1 row created.
SQL> insert into customer values(14,'Srujan','banglore');
1 row created.
SQL> insert into customer values(14,'Raju','Hassan');
1 row created.
SQL> insert into customer values(15,'Rama','Hubballi');
1 row created.
SQL> select *from customer;
CID CNAME ADDRESS
---------- -------------------- --------------------
12 Arjun Tiptur
13 Aryan Tumkur
14 Srujan banglore

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

14 Raju Hassan
15 Rama Hubballi
SQL> SET SERVEROUTPUT ON
SQL> declare
2 cname varchar2(20);
3 cid number(10);
4 begin
5 select cname,cid
6 into cname,cid
7 from customer
8 where cid='18';
9 dbms_output.put_line(cname||' '||cid);
10 exception
11 when no_data_found then
12 dbms_output.put_line('Invalid id');
13 when others then
14 dbms_output.put_line('no records');
15 end;
16 /
Invalid id
PL/SQL procedure successfully completed.

Sowjanya prasad, Dept of Cs


Sscasc,tumkur
[Type here]

Sowjanya prasad, Dept of Cs


Sscasc,tumkur

You might also like