Final Paper - W3 (Solved)
Final Paper - W3 (Solved)
Lahore Campus
2. Write a SQL query that retrieve the employee names who are not certified to
operate any aircraft.
Solution:
SELECT ename
FROM Employees
WHERE Eid NOT IN (SELECT Eid FROM Certified);
Output:
3. Write SQL Query to Retrieve the employee with the highest salary.
Solution:
SELECT *
FROM Employees
WHERE salary = (SELECT MAX(salary) FROM Employees);
Output:
4. Write a SQL Query to retrieve the number of flights departing from each city.
Solution:
5. Write a SQL query to retrieve the employees who are certified to operate an
aircraft with a cruising range greater than 5000 miles.
Solution:
SELECT e.*
FROM Employees e
INNER JOIN Certified c ON e.Eid = c.Eid
INNER JOIN Aircraft a ON c.Aid = a.Aid
WHERE a.cruisingrange > 5000;
Output:
SELECT e.ename
FROM Employees e
JOIN Certified c ON e.Eid = c.Eid
JOIN Aircraft a ON c.Aid = a.Aid
WHERE a.aname LIKE 'Airbus%';
Output:
2. Write a SQL query for all aircraft with cruisingrange over 1,000 miles, find the
name of the aircraft and the average salary of all pilots certified for this aircraft.
Solution:
3. Write a SQL query to find the names of aircraft such that all pilots certified to
operate them earn more than 80,000.
Solution:
4. Write a SQL query for each pilot who is certified for more than three aircraft, find
the eid and the maximum cruisingrange of the aircraft that he or she is certified
for.
Solution:
5. Write a SQL query to retrieve the employees who are certified to operate more
than one aircraft:
Solution:
SELECT e.*
FROM Employees e
JOIN Certified c ON e.Eid = c.Eid
GROUP BY e.Eid
HAVING COUNT(c.Aid) > 1;
Question 3: Write each of the following queries to create Store Procedure in SQL. (2 * 5 = 10)
1. Write store procedure that find Employee Certified for an Aircraft. This accepts an
aircraft ID and returns all employees certified to operate that aircraft.
Solution:
DELIMITER //
CREATE PROCEDURE GetCertifiedEmployeesByAircraftID(Aircraft_id int)
BEGIN
SELECT e.*
FROM Employees e
JOIN Certified c ON e.Eid = c.Eid
WHERE c.Aid = Aircraft_id;
END//
DELIMITER ;
outputFor Aircraft_Id=3:
2. Write a Store Procedure that Finds Flights by Departure Time Range. This stored
procedure accepts start time and end time and returns all flights that depart
within the specified time range.
Solution:
DELIMITER //
CREATE PROCEDURE FindFlightsByDepartureTimeRange(Start_Time time, End_time
time)
BEGIN
SELECT *
FROM Flights
WHERE departs >= Start_Time AND departs <= End_time;
END//
DELIMITER ;
Output for 9:30 and 12:43:
Question 4: Write each of the following queries to create Views in SQL. (2 * 5 = 10)
1. Create a View EmployeeCertificationDetails. This view displays the details of
employees, including their id, name, salary, and the aircraft they are certified to
operate.
Solution:
2. Create a View BusyRoutes. This view shows the busiest routes based on the
number of flights operating between two cities.
Solution: