RDBMS Assignment Questions and Answers
Assignment 1: Consider the following relations:
Student(snum: integer,sname:string,major:string,level:string,age:integer)
Class(name:string,meetsat:string,room:string,fid:integer)
Enrolled(snum:integer,cname:string)
Faculty(fid:integer,fname:string,deptid:integer)
The meaning of these relations is straightforward; for example, Enrolled has one
record per student-class pair such that the student is enrolled in the class.
Write the following queries in SQL. No duplicates should be printed in any of the
answers.
1. Find the names of all Juniors (level = JR) who are enrolled in a class taught by
I. Teach.
2. Find the age of the oldest student who is either a History major or enrolled in a
course taught by I. Teach.
3. Find the names of all classes that either meet in room R128 or have five or more
students enrolled.
4. Find the names of all students who are enrolled in two classes that meet at the
same time.
5. Find the names of faculty members who teach in every room in which some
class is taught.
The answers are given below:
1.
SELECT DISTINCT
[Link]
FROM
Student S, Class C, Enrolled E, Faculty F
WHERE
[Link] = [Link]
AND
[Link] = [Link]
AND
[Link] = [Link]
AND
[Link] = [Link]
AND
[Link] = JR
2.
SELECT
MAX([Link])
FROM
Student S
WHERE
([Link] = History)
OR
[Link]
IN
(
SELECT
[Link]
FROM
Class C, Enrolled E, Faculty F
WHERE
[Link] = [Link]
AND
[Link] = [Link]
AND
[Link] = [Link] )
3.
SELECT
[Link]
FROM
Class C
WHERE
[Link] = R128
OR
[Link]
IN
(
SELECT
[Link]
FROM
Enrolled E
GROUP BY
[Link]
HAVING COUNT(*)>=5)
4.
SELECT DISTINCT
[Link]
FROM
Student S
WHERE
[Link]
IN
(
SELECT
[Link]
FROM
Enrolled E1, Enrolled E2, Class C1, Class C2
WHERE
[Link] = [Link]
AND
[Link]
<>
[Link]
AND
[Link] = [Link]
AND
[Link] = [Link]
AND
[Link]
at = [Link]
at)
5.
SELECT DISTINCT
[Link]
FROM
Faculty F
WHERE NOT EXISTS
((
SELECT
*
FROM
Class C )
EXCEPT
(
SELECT
[Link]
FROM
Class C1
WHERE
[Link] = [Link] ))
Assignment 2: The following relations keep track of airline flight information:
Flights(flno:integer,from:string,to:string,distance:integer,departs:time,arrives
:time,price:real)
Aircraft(aid:integer,aname:string,cruisingrange:integer)
Certified(eid:integer,aid:integer)
Employees(eid:integer,ename:string,salary:integer)
Note that the Employees relation describes pilots and other kinds of employees as
well;every pilot is certified for some aircraft, and only pilots are certified to fly.
Write each of the following queries in SQL.
1. Find the names of aircraft such that all pilots certified to operate them have
salaries more than $80,000.
2. For each pilot who is certified for more than three aircraft, find the
eid
and the
maximum
cruisingrange
of the aircraft for which she or he is certified.
3. Find the names of pilots whose
salary
is less than the price of the cheapest route
from Los Angeles to Honolulu.
4. For all aircraft with
cruisingrange
over 1000 miles, find the name of the aircraft
and the average salary of all pilots certified for this aircraft.
5. Find the names of pilots certified for some Boeing aircraft
The answers are given below:
1.
SELECT DISTINCT
[Link]
FROM
Aircraft A
WHERE
[Link]
IN
(
SELECT
[Link]
FROM
Certified C, Employees E
WHERE
[Link] = [Link]
AND
NOT EXISTS
(
SELECT
*
FROM
Employees E1
WHERE
[Link] = [Link] AND [Link] < 80000 ))
2.
SELECT
[Link],
MAX
([Link])
FROM
Certified C, Aircraft A
WHERE
[Link] = [Link]
GROUP BY
[Link]
HAVING COUNT (*) > 3
3.
SELECT DISTINCT
[Link]
FROM
Employees E
WHERE
[Link] < (
SELECT MIN
([Link])
FROM
Flights F
WHERE
[Link] = Los Angeles
AND
[Link] = Honolulu )
4. Observe that aid is the key for Aircraft, but the question asks for aircraft names; we deal with
this complication by using an intermediate relation Temp:
SELECT
[Link], [Link]
FROM
(
SELECT
[Link], [Link]
AS
name,
AVG
([Link])
AS
AvgSalary
FROM
Aircraft A, Certified C, Employees E
WHERE
[Link] = [Link]
AND
[Link] = [Link]
AND
[Link] > 1000
GROUP BY
[Link], [Link] )
AS
Temp
5.
SELECT DISTINCT
[Link]
FROM
Employees E, Certified C, Aircraft A
WHERE
[Link] = [Link]
AND
[Link] = [Link]
AND
[Link]
LIKE
Boeing%