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: Flightsflno: integer, from: string, to: string, distance: integer, departs: time, arrives: time, price: integer) Aircraftaid: integer, aname: string, cruisingrange: integer) Certifiedeid: integer, aid: integer) Employeeseid: 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.
l. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles. a. m. Print the names of employees who are certified only on aircrafts with cruising range longer Aircraft than 1000 miles, but on at least two such aircrafts. n. Print the A.Aid A A.aname names of employees who are certified only on aircrafts with cruising range longer than 1000 miles Certified and who are certified on some Boeing aircraft. C.eid C.aid = C, E.eid Employees * E Ans: b. C.eid, Employees E1 IN Certified E1.eid = E.eid E1.salary < 80000 )) C.aid = A.aid C, Aircraft A.cruisingrange) A NOT EXISTS c. C.eid Employees *) > 3 MAX E.salary < E E.ename F.price) HAVING COUNT d. Flights F complication Observe that F.from = Los Angeles F.to = Honolulu ) Temp.name, MIN aid by using is the an key intermediate Temp.AvgSalary for A.aid, Aircraft, relation but the Temp: question asks for aircraft names; we deal with this Aircraft A.aname A, Certified name, C, Employees E.salary) E AvgSalary e. A.aid, E.ename = A.aname C.aid ) C.eid Temp = E.eid A.cruisingrange > 1000 AVG f. Employees E.eid A.aid E, Certified C, Aircraft A GROUP Aircraft = C.eid C.aid = A.aid A.aname Boeing% BY A.cruisingrange A > Flights F.from F.distance) = F Los Angeles F.to = Chicago ) LIKE MIN
g. h. i. Flights F F.from, F.to * Employees E.salary > 100000 E Aircraft A.cruisingrange A, Certified > F.distance C NOT EXISTS Employees E E.eid = C.eid A.aid = C.aid) ) Certified C.eid C A.cruisingrange Aircraft A.aid = A C.aid > 3000 Aircraft A1.aid ) A1 F.departs A1.aid = C.aid Flights A1.aname Boeing% )) F.flno F F0.flno Flights F0.from F0.flno Madison F0.to = New York Flights F0.arrives < 18:00 ) IN F0.from F0.to F0, = Madison Flights = F1.from F1.to F0.to = New <> New York York F0.flno F1.departs > F0.arrives Flights F1.arrives < 18:00 ) F0.from F0.to F0, = Madison Flights F1, Flights F2 F1.to F1.from F2.to F0.to F1.to F2.from F1.departs = F2.departs F2.arrives <> New < > 18:00 F0.arrives F1.arrives York )) * E.eid E.ename NOT EXISTS A.aid IN EXISTS NOT EXISTS UNION UNION LIKE
j. C.eid k. C.eid IN E.ename C1.eid m. E.eid, l. n. E.eid, *) E.eid, COUNT HAVING Temp1.avg - Temp2.avg Employees E.eid E.salary) E avg Certified C )) Temp1, E.ename, E.salary Employees E1.salary) ) Temp2 avg AVG IN E.salary Employees E.eid > E Certified C ) AVG Employees E1.eid E1.salary) NOT Employees Certified C1 )) AVG C.aid A.aid E, Certified E.eid C, Aircraft C.eid IN E.ename E.ename Employees A.cruisingrange 1000) C.aid A.aid E, Certified E.eid C, Aircraft C.eid HAVING EVERY E.ename E.ename Employees A.cruisingrange 1000) > 1 C.aid = E.ename A.aid E, Certified C, Aircraft A A.cruisingrange E.eid = > C.eid 1000) A.aname = Boeing ) HAVING EVERY EVERY ANY
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. Empeid: integer, ename: string, age: integer, salary: real) Workseid: integer, did: integer, pct_time: integer) Deptdid: integer, dname: string, budget: real, managerid: integer) Write the following queries in SQL: a. b. Ans: 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 E.ename, managerids E.age 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. E.eid Emp E, Works W1, Works W2, Dept D1, Dept W.did, W1.eid W1.did D1.did D1.dname Hardware Works = W2.eid W2.did = D2.did D2.dname = Software 2000 < W W.eid) Works W1.did W1 = W.did time) ) COUNT HAVING SUM W1.pct
c. d. e. f. g. h. E.ename Emp E.salary E > D.budget Dept D, Works W Dept E.eid = W.eid D.did = W.did) 1000000 D < D.managerid D2.budget ALL E.ename D2.managerid = D.managerid ) E.eid Emp E D.managerid ALL Dept D D.managerid D.budget = D2.budget) Dept Dept D2)) 5000000 D IN < D2.budget) MAX tempd.managerid D2.managerid = D.managerid ) Dept D.managerid D D.budget) tempbudget SUM tempd E.ename tempd.tempbudget = tempd.tempbudget) Emp tempd) E.eid E, = D.managerid Dept D > 1000000) E.Eid, E.ename D.budget < 5000000) MAX ANY Dept Dept ) D.managerid, SUM D.budget HAVING EVERY