SQL Assigment 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Task 1 :

a. Find the names of aircraft such that all pilots certified to operate them earn more than $80,000.

SELECT aname FROM Aircraft WHERE aid IN (SELECT aid FROM Certified GROUP BY aid HAVING
MIN(eid IN (SELECT eid FROM Employees WHERE salary > 80000)) = 1);

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.

SELECT eid, MAX(cruisingrange) AS max_cruisingrange FROM Certified JOIN Aircraft ON


Certified.aid = Aircraft.aid GROUP BY eid HAVING COUNT(*) > 3;

c. Find the names of pilots whose salary is less than the price of the cheapest route from Los Angeles
to Honolulu.

SELECT ename FROM Employees WHERE salary < (SELECT MIN(price) FROM Flights WHERE "from"
= 'Los Angeles' AND "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.

SELECT aname, AVG(salary) AS avg_salary FROM Aircraft JOIN Certified ON Aircraft.aid =


Certified.aid JOIN Employees ON Certified.eid = Employees.eid GROUP BY Aircraft.aid, aname
HAVING cruisingrange > 1000;

e. Find the names of pilots certified for some Boeing aircraft.

SELECT ename FROM Employees WHERE eid IN (SELECT eid FROM Certified JOIN Aircraft ON
Certified.aid = Aircraft.aid WHERE aname LIKE 'Boeing%');

f. Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago.

SELECT DISTINCT aid FROM Flights JOIN Aircraft ON Flights.to = 'Chicago' AND Aircraft.aid =
Flights.flno WHERE Flights."from" = 'Los Angeles';

g. Identify the routes that can be piloted by every pilot who makes more than $10,000.

SELECT "from", "to" FROM Flights GROUP BY "from", "to" HAVING COUNT(*) = (SELECT COUNT(*)
FROM Employees WHERE salary > 10000);
h. Print the enames of pilots who can operate planes with cruisingrange greater than 3000 miles but
are not certified on any Boeing aircraft.

SELECT ename FROM Employees WHERE eid NOT IN (SELECT eid FROM Certified JOIN Aircraft ON
Certified.aid = Aircraft.aid WHERE cruisingrange > 3000 AND aname LIKE 'Boeing%');

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.

SELECT DISTINCT departs FROM Flights WHERE "from" = 'Madison' AND "to" IN (SELECT "from"
FROM Flights WHERE "to" IN (SELECT "from" FROM Flights WHERE "to" = 'New York' AND arrives <
'18:00:00') AND arrives < '18:00:00');

j. Print the names of employees who are certified only on aircrafts with cruisingrange longer than
1000 miles.

SELECT ename FROM Employees WHERE eid IN (SELECT eid FROM Certified JOIN Aircraft ON
Certified.aid = Aircraft.aid GROUP BY eid HAVING MIN(cruisingrange <= 1000) = 0);

k. Print the names of employees who are certified only on aircrafts with cruisingrange longer than
1000 miles, but on at least two such aircrafts.

SELECT ename FROM Employees WHERE eid IN (SELECT eid FROM Certified JOIN Aircraft ON
Certified.aid = Aircraft.aid WHERE cruisingrange > 1000 GROUP BY eid HAVING COUNT(DISTINCT
Aircraft.aid) >= 2);

l. Print the names of employees who are certified only on aircrafts with cruisingrange longer than
1000 miles and who are certified on some Boeing aircraft.

SELECT ename FROM Employees WHERE eid IN (SELECT eid FROM Certified JOIN Aircraft ON
Certified.aid = Aircraft.aid WHERE cruisingrange > 1000 AND aname LIKE 'Boeing%' GROUP BY eid
HAVING COUNT(DISTINCT Aircraft.aid) = (SELECT COUNT(DISTINCT Aircraft.aid) FROM Certified
JOIN Aircraft ON Certified.aid = Aircraft.aid WHERE cruisingrange > 1000 AND aname LIKE 'Boeing
%'));
Task 2:

a. Print the names and ages of each employee who works in both the Hardware department and the
Software department.
SELECT e.ename, e.age FROM Emp e WHERE EXISTS (SELECT * FROM Works w1 WHERE w1.eid =
e.eid AND w1.did = (SELECT did FROM Dept WHERE dname = 'Hardware')) AND EXISTS (SELECT *
FROM Works w2 WHERE w2.eid = e.eid AND w2.did = (SELECT did FROM Dept WHERE dname =
'Software'));

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.
SELECT w.did, COUNT(w.eid) AS num_employees FROM Works w GROUP BY w.did HAVING
SUM(w.pct_time) >= 20;

c. Print the name of each employee whose salary exceeds the budget of all of the departments that
he or she works in.
SELECT e.ename FROM Emp e WHERE salary > ALL (SELECT d.budget FROM Dept d JOIN Works w
ON d.did = w.did WHERE w.eid = e.eid);

d. Find the managerids of managers who manage only departments with budgets greater than $1
million.
SELECT DISTINCT d.managerid FROM Dept d WHERE d.budget > 1000000 AND NOT EXISTS (SELECT
* FROM Dept WHERE managerid = d.managerid AND budget <= 1000000);

e. Find the enames of managers who manage the departments with the largest budgets.
SELECT e.ename FROM Emp e WHERE e.eid IN (SELECT managerid FROM Dept WHERE budget =
(SELECT MAX(budget) FROM Dept));

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.
SELECT managerid FROM (SELECT managerid, SUM(budget) AS total_budget FROM Dept GROUP BY
managerid) AS total WHERE total_budget > 5000000;

g. Find the managerids of managers who control the largest amounts.


SELECT managerid FROM (SELECT managerid, SUM(budget) AS total_budget FROM Dept GROUP BY
managerid) AS total WHERE total_budget = (SELECT MAX(total_budget) FROM (SELECT
SUM(budget) AS total_budget FROM Dept GROUP BY managerid) AS total);

h. Find the enames of managers who manage only departments with budgets larger than $1 million,
but at least one department wwith budget less than $5 million.
SELECT e.ename FROM Emp e WHERE e.eid IN (SELECT DISTINCT managerid FROM Dept WHERE
budget > 1000000 AND EXISTS (SELECT * FROM Dept WHERE managerid = e.eid AND budget <
5000000));

You might also like