SQL Query Example-Solution
SQL Query Example-Solution
Table 2: Courses
Retrieve all students' details who are enrolled in the course "Database
Systems".
SELECT *
FROM Students
WHERE CourseID = (SELECT CourseID FROM Courses WHERE CourseName =
'Database Systems');
3. List the names of all students along with their course names.
SELECT s.Name, c.CourseName
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID;
6. List the courses along with the number of male students enrolled in each
course.
SELECT c.CourseName, COUNT(*) AS MaleStudents
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
WHERE s.Gender = 'M'
GROUP BY c.CourseName;
8. Retrieve the names of students who are older than 21 and are enrolled in
courses taught by Dr. Mehta.
SELECT s.Name
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
WHERE s.Age > 21 AND c.Instructor = 'Dr. Mehta';
9. Find the course names where the average marks of students are greater
than 85.
SELECT c.CourseName
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
GROUP BY c.CourseName
HAVING AVG(s.Marks) > 85;
10. Display the names and marks of students who scored less than the
average marks in their respective courses.
SELECT s.Name, s.Marks
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
WHERE s.Marks < (SELECT AVG(Marks) FROM Students WHERE CourseID =
s.CourseID);
11. List the names of students who are enrolled in more than one course.
SELECT Name
FROM Students
GROUP BY Name
HAVING COUNT(DISTINCT CourseID) > 1;
12. Write a query to find the students who scored the second highest marks
in their course.
SELECT s.Name, s.Marks, c.CourseName
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
WHERE s.Marks = (SELECT MAX(Marks)
FROM Students
WHERE Marks < (SELECT MAX(Marks)
FROM Students
WHERE CourseID = s.CourseID)
AND CourseID = s.CourseID);
13. Display the name of the student and their course who scored the lowest
marks in each course.
SELECT s.Name, s.Marks, c.CourseName
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
WHERE s.Marks = (SELECT MIN(Marks) FROM Students WHERE CourseID =
s.CourseID);
14. List all the courses that have no students enrolled in them.
SELECT CourseName
FROM Courses
WHERE CourseID NOT IN (SELECT DISTINCT CourseID FROM Students);
15. Find pairs of students who are in the same course and display their
names.
SELECT s1.Name AS Student1, s2.Name AS Student2, c.CourseName
FROM Students s1
JOIN Students s2 ON s1.CourseID = s2.CourseID AND s1.StudentID <
s2.StudentID
JOIN Courses c ON s1.CourseID = c.CourseID;
16. Delete the records of students who are enrolled in the course "Database
Systems" and have marks less than 75.
DELETE FROM Students
WHERE CourseID = (SELECT CourseID FROM Courses WHERE CourseName =
'Database Systems')
AND Marks < 75;
17. Find out which course has the maximum number of students.
SELECT c.CourseName, COUNT(*) AS StudentCount
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
GROUP BY c.CourseName
ORDER BY StudentCount DESC
LIMIT 1;
18. Retrieve the names of all female students who are enrolled in courses
that have at least two male students.
SELECT s.Name
FROM Students s
WHERE s.Gender = 'F'
AND s.CourseID IN (SELECT CourseID
FROM Students
WHERE Gender = 'M'
GROUP BY CourseID
HAVING COUNT(*) >= 2);
19. For each course, find the difference between the highest and lowest
marks scored by students.
20. Display the name of the course and the total number of students who
are enrolled in it, but show only the courses that have more than 1 student
enrolled.
SELECT c.CourseName, COUNT(*) AS StudentCount
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
GROUP BY c.CourseName
HAVING COUNT(*) > 1;