本次作业中,由于需要多次匹配employees与aircraft,因此多次复用了下表
aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
1.能驾驶该飞机的所有飞⾏员的⼯资都超过80000美元
SELECT aircraft.aname FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
GROUP BY aircraft.aname
HAVING MIN(employees.salary) >= 80000;
2.对于每个能够驾驶三种以上飞机的飞⾏员,找出其eid和所能驾驶的飞机的最⼤航程
SELECT employees.ename,MIN(aircraft.crusingrange) FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
GROUP BY employees.ename
HAVING COUNT(aircraft.aname) >= 3;
3.找出薪⽔⽐从 Los Angeles 到 Honolulu 的最便宜航线的价格还要少的飞⾏员名字
SELECT employees.ename,employees.salary FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
GROUP BY employees.ename
HAVING employees.salary < (
SELECT MIN(price) FROM flights WHERE origin = 'Los Angeles' AND destination = 'Honolulu'
);
4.对于所有航程⼤于1000英⾥的飞机,找出飞机的名字和可以驾驶这种飞机的所有飞⾏员的平均薪⽔
SELECT aircraft.aname,AVG(employees.salary) FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
WHERE aircraft.crusingrange > 1000
GROUP BY aircraft.aname;
5.找出可以驾驶某种波⾳飞机的飞⾏员名字
SELECT DISTINCT employees.ename FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
WHERE aircraft.aname LIKE 'Boeing %';
6.找出可以⽤在从Los Angeles 到 Chicago 航线上的所有飞机的aids
SELECT * FROM aircraft
WHERE crusingrange >= (
SELECT MIN(distance) FROM flights
WHERE origin = 'Los Angeles' AND destination = 'Chicago'
);
7.找出薪⽔超过100000美元的飞⾏员飞⾏的航线
SELECT DISTINCT employees.ename,flights.origin,flights.destination,flights.departs,flights.arrives
FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
LEFT JOIN flights
ON aircraft.crusingrange >= flights.distance
WHERE employees.salary > 100000
ORDER BY employees.ename;
- 找出那些能驾驶航程⼤于3000英⾥的飞机,但是却没有资格驾驶任何波⾳飞机的飞⾏员的enames
SELECT * FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
AND aircraft.crusingrange > 3000
WHERE employees.ename NOT IN(
SELECT employees.ename FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
WHERE aircraft.aname LIKE 'Boeing %'
);
9.旅客想不超过两次转机从Madison 到 New York 进⾏旅⾏。如果旅客希望6pm到达纽约,列出他应该从
Madison出发的时间
P.S.做这道题时发现了2个小问题:
a.date格式的时间,会截断小时和分钟,因此CREATE TABLE时,最好改成datetime
b.作业的初设INSERT INTO中,43行的arrives比departs要早,莫不是穿越了...
SELECT f1.departs
FROM flights AS f1
LEFT JOIN flights AS f2
ON (
f2.arrives <= '2005/04/12 18:00'
AND f1.destination != 'New York' AND f1.destination = f2.origin
AND f1.arrives <= f2.departs
)
LEFT JOIN flights AS f3
ON (
f3.arrives <= '2005/04/12 18:00'
AND f2.destination != 'New York' AND f2.destination = f3.origin
AND f2.arrives <= f3.departs
)
WHERE f1.origin = 'Madison' AND (f1.destination = 'New York' OR f2.destination = 'New York' OR f3.destination = 'New York')
;
10.计算飞⾏员的平均薪⽔和所有的员⼯(包括飞⾏员)的平均薪⽔之间的差额
SELECT t1.sum_all - t2.sum_certified FROM(
(
SELECT AVG(employees.salary) AS sum_all
FROM employees
) AS t1,
(
SELECT AVG(certified_individual.salary) AS sum_certified
FROM (
SELECT employees.salary
FROM employees
LEFT JOIN certified
ON certified.eid = employees.eid
WHERE certified.eid IS NOT NULL
GROUP BY employees.ename
) AS certified_individual
) AS t2
);
11.打印出那些薪⽔⽐飞⾏员的平均薪⽔要⾼的⾮飞⾏员的名字和薪⽔
SELECT * FROM(
SELECT employees.ename,employees.salary
FROM employees
LEFT JOIN certified
ON certified.eid = employees.eid
WHERE certified.eid IS NULL
GROUP BY employees.ename
) AS uncertified_individual
WHERE uncertified_individual.salary > (
SELECT AVG(certified_individual.salary)
FROM (
SELECT employees.salary
FROM employees
LEFT JOIN certified
ON certified.eid = employees.eid
WHERE certified.eid IS NOT NULL
GROUP BY employees.ename
) AS certified_individual
);
12.打印只能驾驶航程⼤于1000英⾥的飞机的飞⾏员的名字
SELECT employees.ename FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
GROUP BY employees.ename
HAVING MIN(aircraft.crusingrange) > 1000;
13.打印出只能驾驶航程⼤于1000英⾥的飞机,并且能开⾄少两架的飞⾏员的名字
SELECT employees.ename FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
GROUP BY employees.ename
HAVING MIN(aircraft.crusingrange) > 1000 AND COUNT(aircraft.aname) >= 2;
14.打印只能驾驶航程⼤于1000英⾥的波⾳飞机的飞⾏员名字
SELECT employees.ename FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
WHERE employees.eid NOT IN(
SELECT employees.eid FROM aircraft
LEFT JOIN (employees,certified)
ON aircraft.aid = certified.aid
AND employees.eid = certified.eid
WHERE aircraft.aname NOT LIKE 'boeing%' AND certified.eid IS NOT NULL
)
GROUP BY employees.ename
HAVING MIN(aircraft.crusingrange) > 1000;