Assignment 3 Solution
Assignment 3 Solution
p me T
) emana.A ,dia.A di a. C = di a. A
: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
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:
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
di A. A
COUNT
3 > ) *(
di e. C
DISTINCT
MAX
< 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
AS
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
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
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
di a. C = di a. 1 A di a. 1 A
E s e e y ol p m E
0 0 0 0 0 1 > yr al a s. E E s e e y ol p m E *
AND
) )dia.C = dia.A
die.C = die.E
F st h gil F
ot. F , m o r f. F
DISTINCT
e m a n e. E
IN
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
IN
AND
kr o Y w e N = ot. 0 F
AND
) 0 0: 8 1 < s e virr a. 0 F
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
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
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(
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
EVERY
EVERY
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
)die.W(
W s kr o W
( < 0002
, di d. W
di d. W
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
.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
2 D tpe D
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
DISTINCT
di r e g a n a m. D
dir e g a n a m. D
di e. E
E pmE
e m a n e. E
SELECT 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
e m a n e. E , di 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
.g
.f
.e
.d
.c