6 Complete SQL Questions Along With Their Solutions
6 Complete SQL Questions Along With Their Solutions
Previous Papers:
Tables:
[email protected]
2. Sailors & Boats Query (December 2023 - Q4a)
Tables:
1️⃣ Find the sids of all sailors who have reserved red boats but not green boats.
2️⃣ Find all sids of sailors who have a rating of 7 or reserved boat 101.
4️⃣ Find the names of sailors older than the oldest sailor with a rating of 10.
5️⃣ Find the age of the youngest sailor for each rating level.
[email protected]
3. Student & Course Query (July 2023 - Q3a)
Tables:
2️⃣ Find the list of students who have enrolled in the ‘AIML’ course.
4️⃣ List names of all students whose names start with “R”.
5️⃣ List email IDs and mobile numbers of all Computer Science Engineering
students.
[email protected]
4. Employee & Works Query (February 2022 - Q3)
Tables:
UPDATE employee
SET city = 'Newtown'
WHERE employee-name = 'Jones';
UPDATE works
SET salary = salary * 1.10
WHERE company-name = 'First Bank Corporation';
UPDATE works
SET salary = salary * 1.10
WHERE employee-name IN (
SELECT manager-name FROM manages
WHERE employee-name IN (SELECT employee-name FROM works WHERE company-
name = 'First Bank Corporation')
);
4️⃣ Give all managers of ‘First Bank Corporation’ a 10% raise unless their salary
exceeds $100,000; in such cases, give only a 3% raise.
UPDATE works
SET salary = CASE
WHEN salary > 100000 THEN salary * 1.03
ELSE salary * 1.10
END
WHERE employee-name IN (
SELECT manager-name FROM manages
WHERE employee-name IN (SELECT employee-name FROM works WHERE company-
name = 'First Bank Corporation')
);
5️⃣ Delete all tuples in the works relation for employees of ‘Small Bank
Corporation’.
[email protected]
5. Airline & Pilots Query (June/July 2022 - Q4)
Tables:
1️⃣ Find the names of aircraft such that all pilots certified to operate them have
salaries more than $80,000.
2️⃣ Find the names of pilots certified for some Boeing aircraft.
Tables:
1️⃣ Find the names of all Juniors (level = JR) who are enrolled in a class taught by
Prof. Krishna.
[email protected]
2️⃣ Find the age of the oldest student who is either a History major or enrolled in a
course taught by Prof. Joseph.
SELECT MAX(s.age)
FROM Student s
WHERE s.major = 'History'
OR s.snum IN (
SELECT e.snum FROM Enrolled e
JOIN Class c ON e.cname = c.name
JOIN Faculty f ON c.fid = f.fid
WHERE f.fname = 'Joseph'
);
3️⃣ Find the names of all classes that either meet in room R128 or have five or more
students enrolled.
4️⃣ Find the names of faculty members for whom the combined enrollment of the
courses they teach is less than five.
SELECT f.fname
FROM Faculty f
JOIN Class c ON f.fid = c.fid
LEFT JOIN Enrolled e ON c.name = e.cname
GROUP BY f.fname
HAVING COUNT(e.snum) < 5;
5️⃣ For each level, print the level and the average age of students for that level.
6️⃣ Find the names of faculty members who teach in every room in which some
class is taught.
SELECT f.fname
FROM Faculty f
WHERE NOT EXISTS (
SELECT DISTINCT c.room
FROM Class c
WHERE NOT EXISTS (
SELECT * FROM Class c2
WHERE c2.fid = f.fid AND c2.room = c.room
)
);