0% found this document useful (0 votes)
3 views6 pages

6 Complete SQL Questions Along With Their Solutions

The document contains SQL questions and solutions from JNTUK DBMS previous papers, covering various topics including Employee & Department, Sailors & Boats, Student & Course, Employee & Works, Airline & Pilots, and Faculty & Students. Each section includes multiple queries with their corresponding SQL commands to retrieve or manipulate data from specified tables. The queries address tasks such as displaying employee details, modifying records, and aggregating data based on specific conditions.

Uploaded by

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

6 Complete SQL Questions Along With Their Solutions

The document contains SQL questions and solutions from JNTUK DBMS previous papers, covering various topics including Employee & Department, Sailors & Boats, Student & Course, Employee & Works, Airline & Pilots, and Faculty & Students. Each section includes multiple queries with their corresponding SQL commands to retrieve or manipulate data from specified tables. The queries address tasks such as displaying employee details, modifying records, and aggregating data based on specific conditions.

Uploaded by

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

6 complete SQL questions along with their solutions, taken from JNTUK DBMS

Previous Papers:

1. Employee & Department Query (December 2023 - Q3a)

Tables:

• EMPLOYEE (EmpNo, EName, DoB, Address, Gender, Salary, DNo)


• DEPARTMENT (DNo, Dname, ManagerEmpNo, ManagerStartDate)

Questions & Solutions:

1️⃣ Display all employees in the Department named ‘Computer Science’.

SELECT * FROM EMPLOYEE


WHERE DNo = (SELECT DNo FROM DEPARTMENT WHERE Dname = 'Computer Science');

2️⃣ Display the name of the highest salary-paid male employee.

SELECT EName FROM EMPLOYEE


WHERE Gender = 'Male'
ORDER BY Salary DESC
LIMIT 1;

3️⃣ Find the oldest manager in the company.

SELECT ManagerEmpNo, MIN(ManagerStartDate) AS Oldest_Manager_Start_Date


FROM DEPARTMENT;

4️⃣ Display the department name of the employee ‘SRINIVAS’.

SELECT Dname FROM DEPARTMENT


WHERE DNo = (SELECT DNo FROM EMPLOYEE WHERE EName = 'SRINIVAS');

5️⃣ Display the age of female employees.

SELECT EName, YEAR(CURDATE()) - YEAR(DoB) AS Age


FROM EMPLOYEE WHERE Gender = 'Female';

[email protected]
2. Sailors & Boats Query (December 2023 - Q4a)

Tables:

• Sailors (sid, sname, rating, age)


• Boats (bid, bname, color)
• Reserves (sid, bid, day)

Questions & Solutions:

1️⃣ Find the sids of all sailors who have reserved red boats but not green boats.

SELECT DISTINCT r.sid


FROM Reserves r
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'Red'
AND r.sid NOT IN (
SELECT r.sid FROM Reserves r
JOIN Boats b ON r.bid = b.bid
WHERE b.color = 'Green'
);

2️⃣ Find all sids of sailors who have a rating of 7 or reserved boat 101.

SELECT DISTINCT sid FROM Sailors WHERE rating = 7


UNION
SELECT DISTINCT sid FROM Reserves WHERE bid = 101;

3️⃣ Count the number of different sailor names.

SELECT COUNT(DISTINCT sname) FROM Sailors;

4️⃣ Find the names of sailors older than the oldest sailor with a rating of 10.

SELECT sname FROM Sailors


WHERE age > (SELECT MAX(age) FROM Sailors WHERE rating = 10);

5️⃣ Find the age of the youngest sailor for each rating level.

SELECT rating, MIN(age) AS Youngest_Age


FROM Sailors GROUP BY rating;

[email protected]
3. Student & Course Query (July 2023 - Q3a)

Tables:

• Student (Sno, Sname, courseId, email_id, Mobileno)


• Course (CID, Cname, Cduration)

Questions & Solutions:

1️⃣ Add a column city in the Student table.

ALTER TABLE Student ADD city VARCHAR(50);

2️⃣ Find the list of students who have enrolled in the ‘AIML’ course.

SELECT Sname FROM Student


WHERE courseId = (SELECT CID FROM Course WHERE Cname = 'AIML');

3️⃣ List all course names with their durations.

SELECT Cname, Cduration FROM Course;

4️⃣ List names of all students whose names start with “R”.

SELECT Sname FROM Student WHERE Sname LIKE 'R%';

5️⃣ List email IDs and mobile numbers of all Computer Science Engineering
students.

SELECT email_id, Mobileno FROM Student


WHERE courseId = (SELECT CID FROM Course WHERE Cname LIKE '%Computer
Science%');

[email protected]
4. Employee & Works Query (February 2022 - Q3)

Tables:

• employee (employee-name, street, city)


• works (employee-name, company-name, salary)
• company (company-name, city)
• manages (employee-name, manager-name)

Questions & Solutions:

1️⃣ Modify the database so that Jones now lives in Newtown.

UPDATE employee
SET city = 'Newtown'
WHERE employee-name = 'Jones';

2️⃣ Give all employees of ‘First Bank Corporation’ a 10% raise.

UPDATE works
SET salary = salary * 1.10
WHERE company-name = 'First Bank Corporation';

3️⃣ Give all managers of ‘First Bank Corporation’ a 10% raise.

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’.

DELETE FROM works WHERE company-name = 'Small Bank Corporation';

[email protected]
5. Airline & Pilots Query (June/July 2022 - Q4)

Tables:

• Flights (flno, from, to, distance, departs, arrives, price)


• Aircraft (aid, aname, cruisingrange)
• Certified (eid, aid)
• Employees (eid, ename, salary)

Questions & Solutions:

1️⃣ Find the names of aircraft such that all pilots certified to operate them have
salaries more than $80,000.

SELECT DISTINCT aname FROM Aircraft a


WHERE NOT EXISTS (
SELECT * FROM Certified c
JOIN Employees e ON c.eid = e.eid
WHERE a.aid = c.aid AND e.salary <= 80000
);

2️⃣ Find the names of pilots certified for some Boeing aircraft.

SELECT DISTINCT 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 '%Boeing%';

6. Faculty & Students Query (June/July 2022 - Q4)

Tables:

• Student (snum, sname, major, level, age)


• Class (name, meets_at, room, fid)
• Enrolled (snum, cname)
• Faculty (fid, fname, deptid)

Questions & Solutions:

1️⃣ Find the names of all Juniors (level = JR) who are enrolled in a class taught by
Prof. Krishna.

SELECT DISTINCT s.sname


FROM Student s
JOIN Enrolled e ON s.snum = e.snum
JOIN Class c ON e.cname = c.name
JOIN Faculty f ON c.fid = f.fid
WHERE s.level = 'JR' AND f.fname = '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.

SELECT DISTINCT c.name


FROM Class c
WHERE c.room = 'R128'
OR c.name IN (
SELECT e.cname FROM Enrolled e
GROUP BY e.cname
HAVING COUNT(e.snum) >= 5
);

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.

SELECT level, AVG(age) AS avg_age


FROM Student
GROUP BY 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
)
);

[email protected]

You might also like