1. Database Systems
Instructors: Hao-Hua Chu
Winston Hsu
Fall Semester, 2007
Assignment 3: SQL Queries
Questions
1. The following relations keep track of airline flight information:
Flights(flno: integer, from: string, to: string, distance: integer, departs:
time,
arrives: time, price: integer)
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
aswell; every pilot is certified for some aircraft, and only pilots are certified to
fly.Write each of the following queries in SQL.
a. Find the names of aircraft such that all pilots certified to operate them earn
more than $80,000.
b. 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.
c. Find the names of pilots whose salary is less than the price of the cheapest
route from Los Angeles to Honolulu.
d. 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.
e. Find the names of pilots certified for some Boeing aircraft.
f. Find the aids of all aircraft that can be used on routes from Los Angeles to
Chicago.
g. Identify the routes that can be piloted by every pilot who makes more than
$100,000.
h. Print the enames of pilots who can operate planes with cruisingrange greater
than 3000 miles but are not certified on any Boeing aircraft.
i. A customer wants to travel from Madison to New York with no more than two
changes of flight. List the choice of departure times from Madison if the
customer wants to arrive in New York by 6 p.m.
j. Compute the difference between the average salary of a pilot and the average
salary of all employees (including pilots).
k. Print the name and salary of every nonpilot whose salary is more than the
average salary for pilots.
2. ) ’ o g a ci h C ‘ = ot. F AND ’ s el e g n A s o L ‘ = m orf. F WHERE
F st h gil F
FROM
) e c n at si d. F ( SELECT MIN ( > e g n a r g ni si u r c. A WHERE
A tf ar cri A FROM
di a. A SELECT .f
’ %gn ieo B ‘ LIKE e m a n a. A AND dia.A = dia.C AND die.C = die.E WHERE
A tfarcriA ,C deifitreC ,E seeyolpmE FROM
e m a n e. E DISTINCT SELECT .e
p me T AS ) emana.A ,dia.A GROUP BY
0 0 0 1 > e g n ar g ni si ur c. A AND die.E = die.C AND di a. C = di a. A WHERE
E seeyolpmE ,C deifitreC ,A tfarcriA FROM
yr al a S g v A AS ) yr al a s. E( AVG ,e man AS emana.A ,dia.A SELECT ( FROM
y r al a S g v A. p m e T , e m a n. p m e T SELECT
:pmeT noitaler etaidemretni na gnisu yb noitacilpmoc
siht htiw laed ew ;seman tfarcria rof sksa noitseuq eht tub ,tfarcriA rof yek eht si dia taht evresbO .d
) ’ ul ul o n o H ‘ = ot. F AND ’ s el e g n A s o L ‘ = m orf. F WHERE
F st h gil F
FROM
) e cir p. F ( SELECT MIN ( < y r al a s. E WHERE
E s e e y ol p m E FROM
e m a n e. E DISTINCT SELECT .c
3 > ) *( COUNT HAVING
di e. C GROUP BY
di a. A = di a. C WHERE
A tfarcriA ,C deifitreC FROM
) e g n a r g ni si u r c. A ( MAX , di e. C SELECT .b
)) 0 0 0 0 8 < yr al a s. 1 E AND die.E = die.1E WHERE
1 E s e e y ol p m E FROM
* SELECT ( NOT EXISTS
AND die.E = die.C WHERE
E seeyolpmE ,C deifitreC FROM
di a. C
SELECT ( IN di A. A WHERE
A tf ar cri A FROM
e m a n a. A DISTINCT SELECT .a
Ans:
range longer than 1000 miles and who are certified on some Boeing aircraft.
n. Print the names of employees who are certified only on aircrafts with cruising
range longer than 1000 miles, but on at least two such aircrafts.
m. Print the names of employees who are certified only on aircrafts with cruising
range longer than 1000 miles.
l. Print the names of employees who are certified only on aircrafts with cruising
3. )) ’ 0 0: 8 1 ‘ < s e virr a. 2 F AND
s e virr a. 1 F > str a p e d. 2 F AND
s e virr a. 0 F > str a p e d. 1 F AND
’ k r o Y w e N ‘ > < ot. 1 F AND
’ k r o Y w e N ‘ > < ot. 0 F AND
’ kr o Y w e N ‘ = ot. 2 F AND
m orf. 2 F = ot. 1 F AND
m orf. 1 F = ot. 0 F AND
’ n o si d a M ‘ = m orf. 0 F WHERE
2 F st h gil F , 1 F st h gil F , 0 F st h gil F FROM
o nlf. 0 F SELECT (
UNION
) ’ 0 0: 8 1 ‘ < s e virr a. 1 F AND
s e virr a. 0 F > str a p e d. 1 F AND
’ kr o Y w e N ‘ = ot. 1 F AND m orf. 1 F = ot. 0 F AND
’ kr o Y w e N ‘ > < ot. 0 F AND ’ n o si d a M ‘ = m orf. 0 F WHERE
1 F st h gil F , 0 F st h gil F FROM
o nlf. 0 F SELECT (
UNION
) ’ 0 0: 8 1 ‘ < s e virr a. 0 F AND
’ kr o Y w e N ‘ = ot. 0 F AND ’ n o si d a M ‘ = m orf. 0 F WHERE
0 F st h gil F FROM
o nlf. 0 F SELECT (( IN o nl f. F WHERE
F st h gil F FROM
st r a p e d. F SELECT .i
)) ’ % g ni e o B ‘ LIKE e m a n a. 1 A AND
di a. C = di a. 1 A WHERE
1 A tf ar cri A FROM
di a. 1 A (
NOT EXISTS SELECT
AND
) 0 0 0 3 > e g n ar g ni si ur c. A
AND
WHEREdi a. C = di a. A
FROM A tf ar cri A
SELECT di a. A ( WHERE EXISTS
C d eifitr e C
FROM
di e. C
SELECT (( IN di e. E WHERE
E s e e y ol p m E FROM
e m a n e. E DISTINCT SELECT .h
) )dia.C = dia.A AND die.C = die.E
AND e c n at si d. F > e g n ar g ni si ur c. A WHERE
C deifitreC ,A tfarcriA FROM
* SELECT ( NOT EXISTS
AND
0 0 0 0 0 1 > yr al a s. E
WHERE
E s e e y ol p m E
FROM
*
SELECT ( WHERE NOT EXISTS
FROM F st h gil F
ot. F , m o r f. F SELECT DISTINCT .g
4. ) ’ g ni e o B ’ = e m a n a. A( AND ANY ) 0 0 0 1 > e g n a r g ni si u r c. A ( EVERY HAVING
emane.E ,die.E GROUP BY
die.C = die.E AND di a. A = di a. C WHERE
A tfarcriA ,C deifitreC ,E seeyolpmE FROM
e m a n e. E SELECT .n
1 > ) *( AND COUNT ) 0 0 0 1 > e g n a r g ni si u r c. A ( EVERY HAVING
emane.E ,die.E GROUP BY
die.C = die.E AND di a. A = di a. C WHERE
A tfarcriA ,C deifitreC ,E seeyolpmE FROM
e m a n e. E SELECT .m
) 0 0 0 1 > e g n a r g ni si u r c. A ( EVERY HAVING
emane.E ,die.E GROUP BY
die.C = die.E AND di a. A = di a. C WHERE
A tfarcriA ,C deifitreC ,E seeyolpmE FROM
e m a n e. E SELECT l.
) ) 1 C d eifitr e C
FROM
di e. 1 C SELECT DISTINCT (
WHERE IN di e. 1 E
FROM 1 E s e e y ol p m E
SELECT AVG) yr al a s. 1 E( ( > y r al a s. E AND
) C d eifitr e C
FROM
di e. C NOT IN SELECT DISTINCT ( di e. E WHERE
E s e e y ol p m E FROM
yralas.E ,emane.E SELECT .k
AS 2p me T ) 1 E s e e y ol p m E FROM
AVG AS gva ) yr al a s. 1 E( SELECT (
,1p me T )) C d eifitr e C
FROM AS
di e. C
IN SELECT DISTINCT ( di e. E WHERE
E s e e y ol p m E FROM
gva AS ) yr al a s. E( AVG SELECT ( FROM
gva.2pmeT - gva.1pmeT SELECT .j
5. 2. Consider the following relational schema. An employee can work in more than
one department; the pct_time filed 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, dname: string, budget: real, managerid: integer)
Write the following queries in SQL:
a. Print the names and ages of each employee who works in both the Hardware
department and the Software department.
b. 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 did together with the number of employees that
work in that department.
c. Print the name of each employee whose salary exceeds the budget of all of the
departments that he or she works in.
d. Find the managerids of managers who manage only departments with budgets
greater than $1 million.
e. Find the enames of managers who manage the departments with the largest
budgets.
f. 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 million.
g. Find the managerids of managers who control the largest amounts.
h. Find the enames of managers who manage only departments with budgets
larger than $1 million, but at least one department with budget less than $5
million.
Ans:
.a SELECT ega.E ,emane.E
FROM 2 D t p e D , 1 D t p e D , 2 W s kr o W , 1 W s kr o W , E p m E
WHERE di e. 1 W = di e. E AND di d. 1 D = di d. 1 W AND ’ er a w dr a H ‘ = e m a n d. 1 D AND
di e. 2 W = di e. E AND di d. 2 D = di d. 2 W AND ’ er a wtf o S ‘ = e m a n d. 2 D
.b SELECT , di d. W COUNT )die.W(
FROM W s kr o W
GROUP BY di d. W
HAVING ( < 0002 SELECT SUM ) e mit t c p. 1 W(
FROM 1 W s kr o W
WHERE ) di d. W = di d. 1 W
6. ) 0 0 0 0 0 0 5 < t e g d u b. D ( AND ANY ) 0 0 0 0 0 0 1 > t e g d u b. D ( EVERY HAVING
e m a n e. E , di E. E
GROUP BY direganam.D = die.E WHERE
D tpe D , E p m E FROM
e m a n e. E SELECT .h
) D p m et
FROM
)t e g d u B p m et. D p m et( SELECT MAX ( = t e g d u B p m et. D p m et
WHERE
D p m et AS ) di r e g a n a m. D
GROUP BY
FROM D tpe D
t e g d u B p m et AS )t e g d u b. D( SUM , dir e g a n a m. D SELECT DISTINCT ( FROM
dir e g a n a m. D p m et
DISTINCT SELECT .g
) dir e g a n a m. D = dir e g a n a m. 2 D
WHERE
2 D tpe D
FROM
)t e g d u b. 2 D( SELECT SUM ( < 0000005 WHERE
D tpe D FROM
di r e g a n a m. D SELECT .f
)) 2 D t p e D
FROM
)t e g d u b. 2 D( SELECT MAX ( = t e g d u b. D WHERE
D tpe D FROM
dir e g a n a m. D IN SELECT ( di e. E WHERE
E pmE FROM
e m a n e. E SELECT .e
) dir e g a n a m. D = dir e g a n a m. 2 D WHERE
2 D tpe D FROM
tegdub .2 D ALL SELECT ( < 0000001 WHERE
D tpe D FROM
di r e g a n a m. D DISTINCT SELECT .d
) di d. W = di d. D AND di e. W = di e. E WHERE
W s kr o W , D t p e D FROM
t e g d u b. D ALL SELECT ( > y r al a s. E WHERE
E pmE FROM
e m a n e. E SELECT .c