You are on page 1of 28

Experiments List

1. Creation, altering and dropping of tables and inserting rows into a table (use constraints
while creating tables) examples using SELECT command
2. Queries (along with sub-Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS, UNION,
INTERSET, Constraints
3. Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN), GROUP BY,
HAVING and Creation and dropping of Views.
4. Queries using
Conversion functions (to_char, to_number and to_date),
string functions (concatenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length,
substr and instr)
date functions (sysdate, next_day, add_months, last_day,months_between, least,
greatest, trunc, round)
5. Creation of simple PL/SQL program which includes declaration section
executable section and exception handling section (example. Student marks can be
selected from the table and printed for those who secured first class and an exception
can be raised if no records were found)
6. Insert data into student table and use commit and rollback and save point in PL/SQL
block
7. Develop a program that include the features NESTED IF, CASE and CASE
expression.
8. The program can be extended using the NULLIF and COALESCE function
9. Program development using while loops, numeric for loops, nested loops using error
handling, built-in exceptions, user defined exceptions
10. Program development using creation of stored functions, invoke functions in SQL
Statements and write complex functions.
11. Develop programs using features parameters in a CURSOR, FOR UPDATE
CURSOR, WHERE CURRENT of clause and CURSOR variables.
12. Develop Programs using BEFORE and AFTER Triggers, Row and Statement Triggers
and INSTEAD OF Triggers

Page 1
Exp 1: Creation, altering and dropping of tables and inserting rows into a table (use
constraints while creating tables) examples using SELECT command

Page 2
Page 3
Page 4
Page 5
Exp :2 Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS,
UNION,INTERSET, Constraints

1. Find the names of sailors who have reserved boat number 103.
SELECT S.sname FROM Sailors S, Reserves R
WHERE R.bid=103 AND S.sid = R.sid ;

2. Find the sids of sailors who have reserved a red boat.


SELECT R.sid FROM Boats B, Reserves R
WHERE B.color = ‘red' AND B.bid = R.bid

3. Find the names of sailors who have reserved a red boat.


SELECT S.sname FROM Sailors S, Reserves R, Boats B
WHERE B.color = ‘red' AND B.bid = R.bid AND R.sid=S.sid;

4. Find the names of sailors who have reserved a red or a green boat.
(SELECT S.sname FROM Sailors S, Reserves R, Boats B
WHERE B.color = 'red' AND B.bid = R.bid AND R.sid = S.sid )
UNION
(SELECT S.sname FROM Sailors S, Reserves R, Boats B
WHERE B.color = 'green' AND B.bid = R.bid AND R.sid = S.sid );

5. Find all sids of sailors who have a rating of 10 or reserved boat 104.
SELECT S.sid FROM Sailors S
WHERE S.rating = 10
UNION
SELECT R.sid FROM Reserves R
WHERE R.bid = 104;
6. Find the names of sailor's who have reserved both a red and a green boat.
(SELECT S.sname FROM Sailors S, Reserves R, Boats B
WHERE B.color = 'red' AND B.bid = R.bid AND R.sid = S.sid )
INTERSECT
(SELECT S.sname FROM Sailors S, Reserves R, Boats B
WHERE B.color = 'green' AND B.bid = R.bid AND R.sid = S.sid );

Page 6
7. Find the snames of all sailor's who have reserved red boats but not green
(SELECT S.sname FROM Sailors S, Reserves R, Boats B
WHERE B.color = 'red' AND B.bid = R.bid AND R.sid = S.sid )
EXCEPT
(SELECT S.sname FROM Sailors S, Reserves R, Boats B
WHERE B.color = 'green' AND B.bid = R.bid AND R.sid = S.sid );

8. Find the names of sailors who have reserved boat 103.


SELECT S.sname FROM Sailors S
WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103);

9. Find the names of sailors who have reserved a red boat.


SELECT S.sname FROM Sailors S
WHERE S.sid IN
(SELECT R.sid FROM Reserves R
WHERE R. bid IN (SELECT B.bid FROM Boats B WHERE B.color = 'red'));

10. Find the names of sailors who have not reserved a red boat.
SELECT S.sname FROM Sailors S
WHERE S.sid NOT IN (SELECT R.sid FROM Reserves R WHERE R. bid IN
(SELECT B.bid FROM Boats B WHERE B.color = 'red'));

11. Find the names of sailors who have reserved boat number 103.
SELECT S.sname FROM Sailors S
WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 AND R.sid =
S.sid ) ;

12. Find sailors whose rating is better than some sailor called Horatio.
SELECT S.sid
FROM Sailors S WHERE S.rating > ANY ( SELECT S2.rating
FROM Sailors S2
WHERE S2.sname = ‘Horatio’);

13. Find the sailor's with the highest rating.


SELECT S.sid FROM Sailors S WHERE S.rating >= ALL (SELECT S2.rating
FROM Sailors S2);

Page 7
Page 8
Exp 3. Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN),
GROUP BY, HAVING and Creation and dropping of Views.

(1) Find the sum age of all sailors.

SELECT SUM (S.age) FROM Sailors S;

(2) Find the average age of sailors with a rating of 10.

SELECT AVG (S.age) FROM Sailors S WHERE S.rating = 10;

(3) Find the name and age of the oldest sailor.

SELECT S.sname, S.age FROM Sailors S


WHERE S.age=(SELECT MAX(S1.age) FROM Sailors S1);

(4) Count the number of sailors.

SELECT COUNT (*) FROM Sailors S;

(5) Count the number of different sailor names.

SELECT COUNT (DISTINCT S.sname ) FROM Sailors S;

(6) Find the name and age of the youngest sailor.

SELECT S.sname, S.age FROM Sailors S


WHERE S.age=(SELECT MIN(S1.age) FROM Sailors S1);

(7) Find names of sailors who are older than the oldest sailor with a rating of 10.
SELECT S.sname FROM Sailors S
WHERE S.age >
( SELECT MAX ( S2.age ) FROM Sailors S2 WHERE S2.rating = 10 );

(8) Find the age of the youngest sailor for each rating level.

SELECT S.rating, MIN (S.age) FROM Sailors S GROUP BY S.rating;

Page 9
(9) Find the age of the youngest sailor who is eligible to vote (i.e., is at least
18 years old) for each rating level with at least two such sailors.

SELECT S.rating, MIN (S.age) AS minage FROM Sailors S


WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1;

(10) For each red boat, find the number of reservations for this boat.

SELECT B.bid,COUNT (*) AS reservationcount FROM Boats B, Reserves R


WHERE R.bid = B.bid GROUP BY B.bid HAVING B.color = 'red';

Creation and dropping of Views

SQL> Create view v1


As SELECT *
FROM sailors
WHERE rating>5;
SQL>DESC V1;
SQL> SELECT *from v1;
SQL> Drop view v1;

Page 10
Exp :4. Queries using Conversion functions (to_char, to_number and to_date), string
functions (Concatenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length, substr
and instr), date functions (Sysdate, next_day, add_months, last_day,months_between,
least, greatest, trunc, round)

Numeric Functions:

Page 11
String Functions:

Page 12
Date Functions:

Page 13
Conversion Functions:

Page 14
Exp 5. i) Creation of simple PL/SQL program which includes declaration section
executable section and exception handling section (example. Student marks can be
selected from the table and printed for those who secured first class and an exception
can be raised if no records were found)

Exception Case

Page 15
ii) Insert data into student table and use commit and rollback and save point in PL/SQL
block

Page 16
Exp 6. Develop a program that include the features NESTED IF, case and CASE
expression. The program can be extended using the NULLIF and COALESCE function
NESTED IF

Page 17
CASE & CASE EXPRESSION

NULL IF

COALESCE: this function returns the first non-null values among the parameters

Page 18
Page 19
Exp 7. Program development using while loops, numeric for loops, nested loops using
error handling, built-in exceptions, user defined exceptions
While Loop : The Sum Of Individual Digits Of Given Number

For Loop: Print 1 To 10 Numbers

For Loop: Print 1 To 10 Numbers in Reverse Order

Page 20
Built-In Exceptions

User Defined Exceptions

Page 21
Exp 8: Programs development using creation of procedures, passing parameters IN and
OUT of procedures.
Creation of simple procedure

Passing parameters IN and OUT of procedures.

Page 22
Page 23
Exp 9: Program development using creation of stored functions, invoke functions in
SQL Statements and write complex functions.

Page 24
Exp 10: Develop programs using features parameters in a CURSOR, FOR UPDATE
CURSOR, WHERE CURRENT of clause and CURSOR variables.

Implicit cursor

For Update Cursor Loops

Page 25
Exp 11: Develop Programs using BEFORE and AFTER Triggers, Row and Statement
Triggers and INSTEAD OF Triggers

Creation of simple Trigger

Page 26
BEFORE and AFTER using Row level triggers

BEFORE and AFTER using statement level triggers

Page 27
INSTEAD OF TRIGGER

Page 28

You might also like