Document From Sat
Document From Sat
2) Consider the following schemas Sailors(sid: integer, sname: string, rating: integer, age: real);
Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid: integer, day: date).
Answer the following Queries using Aggregate functions, GROUP BY, and HAVING clauses.
a) who is the youngest sailor
select sname from sailors where age=(select min(age) from sailors) ;
-------------------------------------------------------------------------------------------------------------------------------
-
b) Find the name of the sailor who have maximum rating
select sname from sailors where rating=(select max(rating) from sailors);
-------------------------------------------------------------------------------------------------------------------------------
-
c) What is the total rating of all Sailors.
select sum(rating) as total_rating from sailors;
-------------------------------------------------------------------------------------------------------------------------------
d) How many sailors are there with a rating above 9.
select count(*) as num_of_sailors from sailors where rating>9;
-------------------------------------------------------------------------------------------------------------------------------
-
e) Display the number of sailors in each country, sorted from high to low.
SELECT country, COUNT(*) as no_of_sailors
FROM Sailors
GROUP BY country
ORDER BY no_of_sailors DESC;
-------------------------------------------------------------------------------------------------------------------------------
-
f) Display the number of boats in each country, sorted from low to high.
SELECT country, COUNT(*) as no_of_boats
FROM Boats
GROUP BY country
ORDER BY no_of_boats ASC;
3) Consider the following schemas Sailors(sid: integer, sname: string, rating: integer, age: real);
Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid: integer, day: date).
Answer the following Queries using Aggregate functions, GROUP BY, and HAVING clauses.
a) who is the oldest sailor
select sname from sailors where age=(select max(age) from sailors);
-------------------------------------------------------------------------------------------------------------------------------
-
b) Find the name of the sailor who has the minimum rating
select sname from sailors where rating=(select min(rating) from sailors);
-------------------------------------------------------------------------------------------------------------------------------
-
c) What is the average rating of all Sailors
select avg(rating) as average_rating from sailors;
-------------------------------------------------------------------------------------------------------------------------------
-
d) how many sailors are there with a rating above 7.
select count(*) as no_of_sailors from sailors
where rating>7 ;
-------------------------------------------------------------------------------------------------------------------------------
-
e) . Display the number of sailors in each rating, sorted from high to low.
select rating ,count(*) as no_of_sailors
from sailors
GROUP BY rating
ORDER BY no_of_sailors desc;
-------------------------------------------------------------------------------------------------------------------------------
-
f) Display the number of boats in each boatid, sorted from low to high.
SELECT bid, COUNT(*) as no_of_boats
FROM Boat
GROUP BY bid
ORDER BY no_of_boats ASC;
4) Consider the following schemas Sailors(sid: integer, sname: string, rating: integer, age: real);
Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid: integer, day: date).
Answer the following Queries using Aggregate functions, GROUP BY, and HAVING clauses.
a) Find the Sid’s of sailors who have reserved a red or a green boat.
select s.sname from sailors s ,reserve r,boat b where s.sid=r.sid AND r.bid=b.bid AND
b.bcolour='red'
UNION
select s.sname from sailors s ,reserve r,boat b where s.sid=r.sid AND r.bid=b.bid AND
b.bcolour='green';
-------------------------------------------------------------------------------------------------------------------------------
-
b)Find the names of sailors who have reserved a red and a green boat.
select s.sname from sailors s ,reserve r,boat b where s.sid=r.sid AND r.bid=b.bid AND
b.bcolour='red'
intersect
select s.sname from sailors s ,reserve r,boat b where s.sid=r.sid AND r.bid=b.bid AND
b.bcolour='green';
-------------------------------------------------------------------------------------------------------------------------------
-
c) Find the names of sailors who have reserved all boats.
SELECT sname
FROM Sailors s
WHERE NOT EXISTS (
SELECT b.bid
FROM Boat b
WHERE NOT EXISTS (
SELECT r.bid
FROM Reserve r
WHERE r.sid = s.sid AND r.bid = b.bid
)
);
-------------------------------------------------------------------------------------------------------------------------------
-
d)Find the names of sailors who have reserved boat number 103 using correlated nested
query.
select s.sname from sailors s where EXISTS(select * from reserves r where r.bid=103 AND
r.sid = s.sid);
5) Consider the following schemas Sailors(sid: integer, sname: string, rating: integer, age: real);
Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid: integer, day: date).
Answer the following Queries.
a) Find the names of sailors who have reserved a red but not green boats.
select s.sid from sailors s ,reserve r,boat b where s.sid=r.sid AND r.bid=b.bid AND
b.bcolour='red' and b.bcolour!='green';
-------------------------------------------------------------------------------------------------------------------------------
-
b) 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 reserve r where r.bid=104 ;
-------------------------------------------------------------------------------------------------------------------------------
-
c) Find the names of sailors who have reserved boat 103 using independent nested query
select s.sname from sailors s
where sid in (select r.sid from reserve r where r.bid=103);
-------------------------------------------------------------------------------------------------------------------------------
-
d) Find the names of sailors who have reserved a red boat.
select s.sname from sailors s where sid in(select r.sid from reserve r,boat b where
r.bid=b.bid and b.bcolour='red');
-------------------------------------------------------------------------------------------------------------------------------
-
e) Find the names of sailors who have not reserved a red boat
select s.sname from sailors s where sid not in(select r.sid from reserve r,boat b where
r.bid=b.bid and b.bcolour='red');
6) write PL/SQL code for insert trigger, delete trigger, and update trigger using the passenger
database. Passenger (Passport_ id INTEGER PRIMARY KEY, Name VARCHAR (50) Not NULL,
Age Integer Not NULL, Sex Char, Address VARCHAR (50) Not NULL);
a) Write an Insert Trigger to check if the Passport_id is exactly six digits or not.
CREATE TABLE Passenger(Passport_id INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT NOT NULL,
Sex VARCHAR(10),
Addr VARCHAR(10) NOT NULL);
-----------------------------------------------------------
CREATE OR REPLACE TRIGGER checkid
BEFORE
INSERT ON Passenger
FOR EACH ROW
BEGIN
IF
LENGTH(:new.Passport_id) != 6 THEN
RAISE_APPLICATION_ERROR(-20001, 'Passport id has to be 6
digits minimum.');
END IF;
END;
-------------------------------------------------------------------------------------------------------------------------------
-
b) Write a trigger on passenger to display messages ‘1 Record is inserted’, ‘1 record is
deleted’, ‘1 record is updated’ when insertion, deletion and updating are done on passenger
respectively.
CREATE OR REPLACE TRIGGER displayIns
AFTER
INSERT ON Passenger
BEGIN
dbms_output.put_line('1 Record is inserted');
END;
-------------------------------------------------------------------------------------------------------------------------------
-
CREATE OR REPLACE TRIGGER displayUpd
AFTER
UPDATE ON Passenger
BEGIN
dbms_output.put_line('1 Record is updated');
END;
-------------------------------------------------------------------------------------------------------------------------------
-
CREATE OR REPLACE TRIGGER displayDel
AFTER
DELETE ON Passenger
BEGIN
dbms_output.put_line('1 Record is deleted');
END;
-------------------------------------------------------------------------------------------------------------------------------
-
INSERT INTO Passenger VALUES(123456, 'aaa', 12, 'M', 'Sec');
INSERT INTO Passenger VALUES(12345, 'bbb', 13, 'M', 'Secb');
INSERT INTO Passenger VALUES(123457, 'ccc', 18, 'F', 'Hyd');
UPDATE Passenger SET Age=18 WHERE Passport_id = 123456;
DELETE FROM Passenger WHERE Age=18;
7) Consider the following schemas . Sailors(sid: integer, sname: string, rating: integer, age: real);
Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid: integer, day: date).
Answer the following i)Add constraint primary key, foreign key and not null to the reserves table
ii)Insert values into all tables and use commit. iii)Find sailors whose rating is better than some
sailor called ‘Horatio’. iv)Find the names of sailors who have reserved a red and a green boat using
intersect. .v) Find the sailors with the highest rating using all operator.
i) Add constraint primary key, foreign key, and not null to the reserves table:
ALTER TABLE Reserves
ADD CONSTRAINT pk_reserves PRIMARY KEY (sid, bid, day);
iii) Find sailors whose rating is better than some sailor called ‘Horatio’:
SELECT sname
FROM Sailors
WHERE rating > (SELECT rating FROM Sailors WHERE sname = 'Horatio');
-------------------------------------------------------------------------------------------------------------------------------
-
iv) Find the names of sailors who have reserved a red and a green boat using intersect:
SELECT sname
FROM Sailors s
WHERE s.sid IN (
SELECT r.sid
FROM Reserves r
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'red'
)
INTERSECT
SELECT sname
FROM Sailors s
WHERE s.sid IN (
SELECT r.sid
FROM Reserves r
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'green'
);
-------------------------------------------------------------------------------------------------------------------------------
-
v) Find the sailors with the highest rating using all operator:
SELECT sname
FROM Sailors
WHERE rating = (SELECT MAX(rating) FROM Sailors);
---COMPLEX---
8. Write a PL/SQL function which returns number of sailors for a given rating level.
Creation of Function
CREATE OR REPLACE FUNCTION sailcount
(low IN Sailors.Rating%TYPE, high IN Sailors.Rating %TYPE)
RETURN INT IS cnt INT;
BEGIN
SELECT COUNT(*) INTO cnt FROM Sailors WHERE Rating BETWEEN low AND high;
RETURN cnt;
END sailcount;
Execution of Function
DECLARE
L INT;
H INT;
cnt INT;
BEGIN
L := :L;
H := :H;
cnt:= sailcount(L,H);
dbms_output.put_line(cnt);
END;
-------------------------------------------------------------------------------------------------------------------------------
9. write a pl/SQL trigger which will calculate the total marks and percentage of students after
insert/update the details of a student in database.
Creation of Table:
CREATE TABLE Stu (Rollno INT PRIMARY KEY, Name VARCHAR(10), M1 INT, M2 INT, M3
INT, TOTAL INT, PERCENT FLOAT);
Creation of Trigger:
CREATE OR REPLACE TRIGGER stutotal
BEFORE
INSERT ON Stu
FOR EACH ROW
BEGIN
:new.TOTAL := :new.M1+:new.M2+:new.M3;
:new.PERCENT := :new.TOTAL/3;
END;
Insertion of Records:
INSERT INTO Stu(Rollno, Name, M1, M2,M3) values(503, 'AAA', 20, 30, 80);
INSERT INTO Stu(Rollno, Name, M1, M2,M3) values(502, 'AAA', 100, 90, 60);
INSERT INTO Stu(Rollno, Name, M1, M2,M3) values(501, 'AAA', 50, 40, 40);
Displaying of Table:
SELECT * FROM Stu;
-------------------------------------------------------------------------------------------------------------------------------
10.write a PL/SQL procedure to check whether the given number is prime or not.
Creation of Procedure
CREATE OR REPLACE PROCEDURE isprime
(A IN INT) IS
FLAG INT := 0;
BEGIN
FOR COUNTER IN 2..A-1 LOOP
IF A MOD COUNTER = 0 THEN
dbms_output.put_line('Not Prime');
FLAG:=1; EXIT;
END IF;
END LOOP;
IF FLAG = 0 THEN
dbms_output.put_line('Prime');
END IF;
END isprime;
Execution of Procedure
DECLARE
A INT;
BEGIN
A:= :A;
isprime(A);
END;
-------------------------------------------------------------------------------------------------------------------------------
11.write a PL/SQL function which returns average age of sailors for a given rating level.
Creation of Function
CREATE OR REPLACE FUNCTION avgage
(low IN Sailors.Rating%TYPE, high IN Sailors.Rating %TYPE)
RETURN INT IS
cnt INT;
BEGIN
SELECT AVG(Age) INTO cnt FROM Sailors WHERE Rating BETWEEN low AND high;
RETURN cnt;
END avgage;
Execution of Function
DECLARE
L INT;
H INT;
cnt INT;
BEGIN L := :L;
H := :H;
cnt:= avgage(L,H);
dbms_output.put_line(cnt);
END;
-------------------------------------------------------------------------------------------------------------------------------
12.Write a PL/SQL block using implicit cursor that will display message, the salaries of all the
employees in the ‘employee’ table are updated. If none of the employee’s salary are updated, we get
a message 'None of the salaries were updated'. Else we get a message like for example, 'Salaries for
1000 employees are updated' if there are 1000 rows in ‘employee’table.
DECLARE
cnt NUMBER(10) := 0;
BEGIN
UPDATE emp SET sal = sal* 1.1;
cnt:= SQL%ROWCOUNT;
IF cnt = 0 THEN
DBMS_OUTPUT.PUT_LINE('None of the salaries were updated.');
ELSE
DBMS_OUTPUT.PUT_LINE('Salaries for ' || cnt || ' employees are updated.');
END IF;
end;
-------------------------------------------------------------------------------------------------------------------------------
13. Write a Cursor to find an employee with the given job and dept no.
declare
dno number(10):= :Dept_No;
j varchar2(32):= :JOB;
cursor c5 is select * from emp where job=j and
deptno=dno;
rec emp%rowtype;
begin open c5;
dbms_output.put_line('....LIST OF EMPLOYEES ----');
loop
fetch c5 into rec;
exit when c5%notfound;
dbms_output.put_line( 'Emp name='||rec.ename||' JOB=' || rec.job || ' Dept No= '||rec.deptno);
end loop;
close c5;
end;
-------------------------------------------------------------------------------------------------------------------------------
14. Write PL/SQL a function which returns week day of a given date .
Creation of Function
CREATE OR REPLACE FUNCTION weekday
(date IN DATE) RETURN VARCHAR IS
day VARCHAR(10);
BEGIN
day := TO_CHAR(date, 'DAY');
RETURN day;
END;
Execution of Function
DECLARE
A DATE;
day VARCHAR(10);
BEGIN A:= :A;
day := weekday(A);
dbms_output.put_line('Weekday: '||day);
END;
--ADDITIONAL—
15. Write a Cursor to Display the employee names and their salary for the given department
number.
DECLARE
CURSOR c1 IS SELECT * FROM emp WHERE deptno = :D_NO;
rec emp%rowtype;
BEGIN
OPEN c1;
LOOP
fetch c1 into rec;
exit when c1%notfound;
dbms_output.put_line('Dept No: ' ||rec.deptno || 'Employee Name: '||rec.ename|| ' Employee Salary:
'||rec.sal);
END LOOP;
CLOSE c1;
END;
-------------------------------------------------------------------------------------------------------------------------------
16. write a procedure to find the lucky number of a given birth date .
Creation of Procedure:
CREATE OR REPLACE PROCEDURE GetLuckyNumber (
p_birth_date IN DATE
) AS
v_lucky_number NUMBER := 0;
BEGIN
FOR i IN 1 .. LENGTH(TO_CHAR(p_birth_date, 'DDMMYYYY')) LOOP
v_lucky_number := v_lucky_number +
TO_NUMBER(SUBSTR(TO_CHAR(p_birth_date, 'DDMMYYYY'), i, 1));
END LOOP;
DBMS_OUTPUT.PUT_LINE('Lucky Number for ' || TO_CHAR(p_birth_date, 'DD-MM-YYYY') || '
is: ' || v_lucky_number);
END GetLuckyNumber;
DECLARE
is_pal BOOLEAN;
BEGIN
check_palindrome(12321, is_pal);
IF is_pal THEN
DBMS_OUTPUT.PUT_LINE('The number is a palindrome.');
ELSE
DBMS_OUTPUT.PUT_LINE('The number is not a palindrome.');
END IF;
END;
-------------------------------------------------------------------------------------------------------------------------------
20.Write a Cursor to find an employee with the given job and dept no.
declare
dno number(10):= :Dept_No;
j varchar2(32):= :JOB;
cursor c5 is select * from emp where job=j and
deptno=dno;
rec emp%rowtype;
begin open c5;
dbms_output.put_line('....LIST OF EMPLOYEES ----');
loop
fetch c5 into rec;
exit when c5%notfound;
dbms_output.put_line( 'Emp name='||rec.ename||' JOB=' || rec.job || ' Dept No= '||rec.deptno);
end loop;
close c5;
end;
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------