You are on page 1of 4

1.

Write a program in Java to create following tables and write sql queries to perform given tasks in
Oracle
Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer)
a. Find the names and ages of all sailors
SELECT SNAME,AGE FROM SAILORS
b. Find all sailors with a rating above 7
SELECT SNAME FROM SAILORS WHERE RATING>7
c. Find the names of sailors who have reserved boat number 103
SELECT SNAME FROM SAILORS WHERE SID IN(SELECT SID FROM RESERVES WHERE
BID =103)
d. Find the sids of sailors who have reserved a red boat
SELECT S.SID FROM SAILORS S,RESERVES R,BOATS B WHERE S.SID=R.SID AND
R.BID=B.BID AND B.COLOR='RED'
e. Find the colors of boats reserved by Raj
SELECT B.COLOR FROM SAILORS S,RESERVES R,BOATS B WHERE S.SID=R.SID AND
R.BID=B.BID AND S.SNAME='RAJ'
f. Find the names of sailors who have reserved at least one boat
SELECT DISTINCT S.SNAME FROM SAILORS S,RESERVES R WHERE S.SID=R.SID
g. Find the names of sailors who have reserved a red or green boat
SELECT S.SNAME FROM SAILORS S,RESERVES R,BOATS B WHERE S.SID=R.SID AND
R.BID=B.BID AND (B.COLOR='RED' OR B.COLOR='GREEN')
h. Find the names of sailors who have reserved both a red and a green boat
SELECT S1.SNAME FROM SAILORS S1,RESERVES R1,BOATS B1 WHERE S1.SID=R1.SID
AND R1.BID=B1.BID AND B1.COLOR='RED'
INTERSECT
SELECT S2.SNAME FROM SAILORS S2, RESERVES R2,BOATS B2 WHERE S2.SID=R2.SID
AND R2.BID=B2.BID AND B2.COLOR='GREEN'

i. Find sids of all sailors who have reserved red boats but not green boats

j. Find all sids of sailors who have a rating of 10 or reserved boat 104
SELECT SID FROM SAILORS WHERE RATING=10 UNION SELECT SID FROM RESERVES
WHERE BID =104
k. Find the names of sailors who have not reserved a red boat
SELECT SNAME FROM SAILORS WHERE SID NOT IN(SELECT SID FROM RESERVES
WHERE BID IN(SELECT BID FROM BOATS WHERE COLOR='RED'))
l. Find sailors whose rating is better than some sailor called Ram.
SELECT S.SID FROM SAILORS WHERE S.RATING > ANY(SELECT S2.RATING FROM
SAILORS S2 WHERE S2.NAME=’RAM’)
m. Find the names of sailors who have reserved all boats
SELECT S.SNAME FROM SAILORS S WHERE
NOT EXISTS(SELECT B.BID FROM BOATS B WHERE
NOT EXISTS (SELECT R.BID FROM RESERVES R WHERE R.BID=B.BID
AND R.SID=S.SID))
n. Find the average age of all sailors
SELECT AVG(AGE) FROM SAILORS
o. Find the average age of sailors with a rating of 10
SELECT AVG(AGE) FROM SAILORS WHERE RATING =10
p. Find the name and age of the oldest sailor
SELECT SNAME,AGE FROM SAILORS WHERE AGE IN(SELECT MAX(AGE) FROM SAILORS
)
q. Count the number of sailors
SELECT COUNT(SID) FROM SAILORS
r. Count the number of different sailor names
SELECT DISTINCT COUNT(SNAME) FROM SAILORS
s. Find the names of sailors who are older than the oldest sailor with rating of 10
SELECT S.SNAME FROM SAILORS S WHERE S.AGE> (SELECT MAX(S2.AGE) FROM
SAILORS S2 WHERE RATING =10)
t. Find the age of the youngest sailor for each rating level
SELECT RATING,MIN(AGE) FROM SAILORS GROUP BY RATING
u. Find the age of youngest sailor who is at least 18 years old
SELECT MIN(AGE) FROM SAILORS WHERE AGE>=18
v. For each red boat, find the number of reservations for this boat
SELECT COUNT(R.BID) FROM RESERVES R,BOATS B WHERE R.BID=B.BID AND
B.COLOR='RED'
w. Find average age of sailors for each rating level.
SELECT AVG(AGE) FROM SAILORS GROUP BY RATING
x. Find those ratings for which the average age of sailors is 18
5. Consider the following relational schema. An employee can work in more than one department; the pct time
_eld of the Works relation shows the percentage of time that a given employee works in a given department.
Emp(eid: integer, ename: string, age: integer, salary: real)
Works(eid: integer, did: integer, pct time: integer)
Dept(did: integer, budget: real, managerid: integer)
Write the following queries in SQL:

1. Print the names and ages of each employee who works in both the Hardware department and the
Software department.
SELECT E.ENAME,E.AGE FROM EMP1 E,WORKS W,DEPT1 D WHERE E.EID=W.EID AND
W.DID=D.DID AND D.DNAME='SOFTWARE'
INTERSECT
SELECT E.ENAME,E.AGE FROM EMP1 E,WORKS W2,DEPT1 D2 WHERE E.EID=W2.EID
AND W2.DID=D2.DID AND D2.DNAME='HARDWARE'

SELECT E.ENAME,E.AGE FROM EMP1 E,WORKS W,WORKS W2,DEPT1 D2,DEPT1 D WHERE


E.EID=W.EID AND W.DID=D.DID AND D.DNAME='SOFTWARE' AND E.EID=W2.EID AND
W2.DID=D2.DID AND D2.DNAME='HARDWARE'

2. For each department with more than 20 full-time-equivalent employees (i.e., where the part-time and
full-time employees add up to at least that many full-time employees), print the didtogether with the
number of employees that work in that department.

3. Print the name of each employee whose salary exceeds the budget of all of the departments that he or
she works in.
SELECT E.ENAME FROM EMP1 E WHERE E.SALARY > ALL(SELECT D.BUDGET FROM
DEPT1 D,WORKS W WHERE E.EID=W.EID AND D.DID=W.DID)

4. Find the managerids of managers who manage only departments with budgets greater than $1,000,000.
SELECT MANAGERID FROM DEPT1 WHERE BUDGET>1000000

5. Find the enames of managers who manage the departments with the largest budget.
SELECT E.ename
FROM Emp1 E
WHERE E.eid IN (SELECT D.managerid
FROM Dept1 D
WHERE D.budget = (SELECT MAX (D2.budget)
FROM Dept1 D2))

6. If a manager manages more than one department, he or she controls the sum of all the budgets for those
departments. Find the managerids of managers who control more than $5,000,000.
SELECT D.managerid
FROM Dept1 D
WHERE 5000000 < (SELECT SUM (D2.budget)
FROM Dept1 D2
WHERE D2.managerid = D.managerid )

7. Find the managerids of managers who control the largest amount.


3. Consider the following schema:
Suppliers(sid: integer, sname: string, address: string)
Parts(pid: integer, pname: string, color: string)
Catalog(sid: integer, pid: integer, cost: real)

1. Find the pnames of parts for which there is some supplier.

2. Find the snames of suppliers who supply every part.

3. Find the snames of suppliers who supply every red part.


SELECT C.SID FROM CATALOG C WHERE
NOT EXISTS (SELECT P.PID FROM PARTS P WHERE P.COLOR='RED' AND (NOT EXISTS (SELECT
C1.SID FROM CATALOG C1 WHERE C1.SID=C.SID AND C1.PID=P.PID)))

4. Find the pnames of parts supplied by Acme Widget Suppliers and by no one else.

5. Find the sids of suppliers who charge more for some part than the average cost of thatpart (averaged over all
the suppliers who supply that part).

6. For each part, Find the snameof the supplier who charges the most for that part.

8. Find the sids of suppliers who supply only red parts.


SELECT S.SID FROM SUPPLIERS,PARTS P,

9. Find the sids of suppliers who supply a red part and a green part.
SELECT S.SID FROM SUPPIERS S,PARTS P,CATALOG C WHERE S.SID=C.SID AND
C.PID=P.PID AND P.COLOR=’RED’
INTERSECT
SELECT S.SID FROM SUPPLIERS S2,PARTS P2,CATALOG C2 WHERE S2.SID=C2.SID AND
C2.PID=P2.PID AND P2.COLOR=’GREEN’

10. Find the sids of suppliers who supply a red part or a green part
SELECT S.SID FROM SUPPIERS S,PARTS P,CATALOG C WHERE S.SID=C.SID AND
C.PID=P.PID AND P.COLOR=’RED’
UNION
SELECT S.SID FROM SUPPLIERS S2,PARTS P2,CATALOG C2 WHERE S2.SID=C2.SID AND
C2.PID=P2.PID AND P2.COLOR=’GREEN’

You might also like