0% found this document useful (0 votes)
61 views9 pages

Final Paper - W3 (Solved)

The document describes a final exam for a Database Systems lab course. It includes instructions, a relational schema describing employees, aircrafts, certifications, and flights, and 5 questions assessing various SQL skills. Question 1 has students write 5 SELECT queries. Question 2 has students write 5 more advanced SELECT queries. Question 3 has students write 2 stored procedures. Question 4 has students write 2 views. Question 5 has students write a trigger to prevent negative flight prices. The exam tests a range of SQL skills including queries, stored procedures, views, and triggers on the given schema.

Uploaded by

MASTER JII
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views9 pages

Final Paper - W3 (Solved)

The document describes a final exam for a Database Systems lab course. It includes instructions, a relational schema describing employees, aircrafts, certifications, and flights, and 5 questions assessing various SQL skills. Question 1 has students write 5 SELECT queries. Question 2 has students write 5 more advanced SELECT queries. Question 3 has students write 2 stored procedures. Question 4 has students write 2 views. Question 5 has students write a trigger to prevent negative flight prices. The exam tests a range of SQL skills including queries, stored procedures, views, and triggers on the given schema.

Uploaded by

MASTER JII
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

University of Management and Technology,

Lahore Campus

Final Examination : Spring 2023


Course Name: Database Systems (Lab) Course Code: CC2141L Credit Hours: 4(3,1)
Course Instructor/s: Mr. Waqar Ashiq Program Name BS Software Engineering
Semester/ Section: BSSE 4, W6 Maximum Marks: 50
Submission Jun 12, 2023 at 1:00 PM
Moderator Signature
Deadline: Total Time (1 Hour 30 min)
Studend Name: Student ID:
Instructions:
 Understanding the problem is a part of the task.
 Submit your paper on LMS within the given time frame. Emailed solutions will not be accepted.
 Attempt all the questions.
Questions: CLO-1, CLO-2
Consider the Relational Schema given below. Note that the Employee's relation describes
pilots and other kinds of employees as well every pilot is certified for some aircraft, and only

pilots are certified to fly.


Question 1: Write each of the following queries in SQL. (5 * 2 = 10)
1. Write a SQL query to Retrieve the total distance covered by all the flights.
Solution:

SELECT SUM(distance) AS total_distance


FROM Flights;
Output:

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:

SELECT ffrom AS departure_city, COUNT(*) AS num_flights


FROM Flights
GROUP BY ffrom;
Output:

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:

Question 2: Write each of the following queries in SQL. (5 * 3 = 15)


1. Write a SQL Query to find the names of employees who are certified to operate
Airbus aircraft.
Solution:

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:

SELECT a.aname AS aircraft_name, AVG(e.salary) AS average_salary


FROM Aircraft a
JOIN Certified c ON a.Aid = c.Aid
JOIN Employees e ON c.Eid = e.Eid
WHERE a.cruisingrange > 1000
GROUP BY a.aname;

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:

SELECT a.aname AS aircraft_name


FROM Aircraft a
JOIN Certified c ON a.Aid = c.Aid
JOIN Employees e ON c.Eid = e.Eid
GROUP BY a.aname
HAVING MIN(e.salary) > 80000;

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:

SELECT c.Eid, MAX(a.cruisingrange) AS max_cruisingrange


FROM Certified c
JOIN Aircraft a ON c.Aid = a.Aid
GROUP BY c.Eid
HAVING COUNT(c.Aid) > 3;

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:

CREATE VIEW EmployeeCertificationDetails AS


SELECT e.Eid, e.ename, e.salary, a.aname AS certified_aircraft
FROM Employees e
JOIN Certified c ON e.Eid = c.Eid
JOIN Aircraft a ON c.Aid = a.Aid;

2. Create a View BusyRoutes. This view shows the busiest routes based on the
number of flights operating between two cities.
Solution:

CREATE VIEW BusyRoutes AS


SELECT f.ffrom AS departure_city, f.Fto AS destination_city, COUNT(*) AS flight_count
FROM Flights f
GROUP BY f.ffrom, f.Fto
ORDER BY flight_count DESC;

Question 5: Write a SQL query for Trigger in SQL. (5)


1. Create a Trigger: PreventNegativePrice. This trigger prevents updating the price of
a flight to a negative value.
Solution:

Place Query with Output Here

You might also like