Sei sulla pagina 1di 6

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.

A tfarcriA ,C deifitreC ,E seeyolpmE

p me T

) emana.A ,dia.A di a. C = di a. A

E seeyolpmE ,C deifitreC ,A tfarcriA

: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 ) ul ul o n o H = ot. F s el e g n A s o L = m orf. F ) e cir p. F ( .d

F st h gil F

di a. A = di a. C A tfarcriA ,C deifitreC ) e g n a r g ni si u r c. A ( )) 0 0 0 0 8 < yr al a s. 1 E die.E = die.1E * , di e. C

1 E s e e y ol p m E

die.E = die.C di a. C

E seeyolpmE ,C deifitreC

A tf ar cri A

Ans:

SELECT FROM WHERE

l. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles. m. 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. n. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles and who are certified on some Boeing aircraft.

DISTINCT

e m a n a. A

IN

SELECT FROM WHERE NOT EXISTS

di A. A

SELECT FROM WHERE GROUP BY HAVING

COUNT

3 > ) *(

di e. C

SELECT FROM WHERE

DISTINCT

MAX

SELECT MIN FROM WHERE

< y r al a s. E E s e e y ol p m E

e m a n e. E

SELECT FROM

y r al a S g v A. p m e T , e m a n. p m e T

SELECT FROM WHERE GROUP BY

AS

AND SELECT FROM WHERE

AVG

AND

AS

yr al a S g v A

) yr al a s. E(

,e man

AND AS

AND

0 0 0 1 > e g n ar g ni si ur c. A

die.E = die.C

emana.A ,dia.A

SELECT FROM WHERE

DISTINCT

e m a n e. E

AND

AND

LIKE

%gn ieo B

e m a n a. A

dia.A = dia.C

die.C = die.E

SELECT FROM WHERE

SELECT MIN FROM WHERE

AND AND

) o g a ci h C = ot. F

s el e g n A s o L = m orf. F F st h gil F ) e c n at si d. F (

( > e g n a r g ni si u r c. A A tf ar cri A di a. A

.f

.e

.c

.b

.a

)) 0 0: 8 1 < s e virr a. 2 F s e virr a. 1 F > str a p e d. 2 F s e virr a. 0 F > str a p e d. 1 F k r o Y w e N > < ot. 1 F k r o Y w e N > < ot. 0 F kr o Y w e N = ot. 2 F m orf. 2 F = ot. 1 F m orf. 1 F = ot. 0 F

n o si d a M = m orf. 0 F 2 F st h gil F , 1 F st h gil F , 0 F st h gil F o nlf. 0 F ) 0 0: 8 1 < s e virr a. 1 F s e virr a. 0 F > str a p e d. 1 F kr o Y w e N = ot. 1 F kr o Y w e N > < ot. 0 F m orf. 1 F = ot. 0 F

n o si d a M = m orf. 0 F 1 F st h gil F , 0 F st h gil F o nlf. 0 F

di a. C = di a. 1 A di a. 1 A

E s e e y ol p m E

e c n at si d. F > e g n ar g ni si ur c. A C deifitreC ,A tfarcriA *

0 0 0 0 0 1 > yr al a s. E E s e e y ol p m E *

SELECT DISTINCT FROM WHERE NOT EXISTS

SELECT FROM WHERE AND NOT EXISTS

SELECT FROM WHERE

AND

) )dia.C = dia.A

die.C = die.E

F st h gil F

ot. F , m o r f. F

SELECT FROM WHERE

DISTINCT

e m a n e. E

IN

SELECT FROM WHERE EXISTS

SELECT FROM WHERE AND AND NOT EXISTS SELECT FROM WHERE AND

LIKE

)) % g ni e o B

e m a n a. 1 A

1 A tf ar cri A

) 0 0 0 3 > e g n ar g ni si ur c. A di a. C = di a. A di a. A A tf ar cri A

C d eifitr e C di e. C

((

di e. E

SELECT FROM WHERE

IN

SELECT FROM WHERE

AND

kr o Y w e N = ot. 0 F

n o si d a M = m orf. 0 F 0 F st h gil F o nlf. 0 F

AND

) 0 0: 8 1 < s e virr a. 0 F

UNION SELECT FROM WHERE


(

UNION SELECT FROM WHERE

AND AND AND AND AND AND AND AND AND AND AND

AND AND

((

o nl f. F

F st h gil F

st r a p e d. F

.i

.h

AND

.g

emane.E ,die.E

A tfarcriA ,C deifitreC ,E seeyolpmE e m a n e. E 1 > ) *( ) 0 0 0 1 > e g n a r g ni si u r c. A ( die.C = die.E

emane.E ,die.E di a. A = di a. C e m a n e. E ) 0 0 0 1 > e g n a r g ni si u r c. A ( die.C = die.E

A tfarcriA ,C deifitreC ,E seeyolpmE

emane.E ,die.E di a. A = di a. C e m a n e. E ) ) 1 C d eifitr e C di e. 1 C A tfarcriA ,C deifitreC ,E seeyolpmE

di e. 1 E

1 E s e e y ol p m E

) C d eifitr e C di e. C

2p me T gva

) 1 E s e e y ol p m E

,1p me T

)) C d eifitr e C

SELECT FROM

gva.2pmeT - gva.1pmeT

SELECT FROM WHERE

IN SELECT DISTINCT FROM AS AVG AS AS

di e. C

di e. E

E s e e y ol p m E

AVG

AS

gva

) yr al a s. E(

SELECT FROM

) yr al a s. 1 E(

SELECT FROM WHERE

NOT IN SELECT DISTINCT FROM SELECT AVG FROM WHERE IN SELECT DISTINCT FROM

di e. E

E s e e y ol p m E

yralas.E ,emane.E

AND

) yr al a s. 1 E(

( > y r al a s. E

.k

.j

l.

SELECT FROM WHERE GROUP BY HAVING

SELECT FROM WHERE GROUP BY HAVING SELECT FROM WHERE GROUP BY HAVING

EVERY

EVERY

AND AND AND

die.C = die.E

di a. A = di a. C

EVERY

AND ANY

) g ni e o B = e m a n a. A(

) 0 0 0 1 > e g n a r g ni si u r c. A (

.n

AND COUNT

.m

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 fulltime 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:
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 ) e mit t c p. 1 W( ) di d. W = di d. 1 W 1 W s kr o W ega.E ,emane.E

SELECT FROM WHERE

)die.W(

W s kr o W

( < 0002

, di d. W

di d. W

SELECT FROM GROUP BY HAVING

COUNT

SUM

er a w dr a H = e m a n d. 1 D er a wtf o S = e m a n d. 2 D

di d. 1 D = di d. 1 W di d. 2 D = di d. 2 W

di e. 2 W = di e. E

di e. 1 W = di e. E

SELECT FROM WHERE

.b

.a

AND AND

AND AND

AND

) D p m et )t e g d u B p m et. D p m et(

( = t e g d u B p m et. D p m et ) di r e g a n a m. D D tpe D

) dir e g a n a m. D = dir e g a n a m. 2 D )t e g d u b. 2 D(

2 D tpe D

)) 2 D t p e D )t e g d u b. 2 D(

( = t e g d u b. D D tpe D

) dir e g a n a m. D = dir e g a n a m. 2 D tegdub .2 D

2 D tpe D

SELECT FROM WHERE

ALL SELECT FROM WHERE

AND

) di d. W = di d. D

di e. W = di e. E t e g d u b. D

W s kr o W , D t p e D

> y r al a s. E E pmE e m a n e. E

SELECT FROM WHERE

DISTINCT

ALL SELECT FROM WHERE

< 0000001 D tpe D

di r e g a n a m. D

SELECT FROM WHERE

IN SELECT FROM WHERE

dir e g a n a m. D

di e. E

E pmE

e m a n e. E

SELECT FROM WHERE

SELECT SUM FROM WHERE

< 0000005 D tpe D di r e g a n a m. D

SELECT FROM

DISTINCT SELECT DISTINCT FROM GROUP BY WHERE

SELECT MAX FROM

SUM

AS

t e g d u B p m et

)t e g d u b. D(

, dir e g a n a m. D

AS

D p m et

dir e g a n a m. D p m et

SELECT FROM WHERE HAVING

GROUP BY AND ANY

e m a n e. E , di E. E

direganam.D = die.E D tpe D , E p m E e m a n e. E

EVERY

) 0 0 0 0 0 0 5 < t e g d u b. D (

) 0 0 0 0 0 0 1 > t e g d u b. D (

.h

SELECT MAX FROM

.g

.f

.e

.d

.c

Potrebbero piacerti anche