Table 1: Students
StudentI Name Ag Gender CourseI Marks
D e D
1 Arjun 21 M 101 85
2 Priya 22 F 102 90
3 Varun 20 M 101 78
4 Neha 23 F 103 82
5 Ravi 21 M 102 88
Table 2: Courses
CourseI CourseName Instructor
D
101 Database Systems Dr. Sharma
102 Data Structures Dr. Mehta
103 Operating Systems Dr. Raj
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');
2. Display the names of students who scored more than 80 marks.
SELECT Name
FROM Students
WHERE Marks > 80;
3. List the names of all students along with their course names.
SELECT [Link], [Link]
FROM Students s
JOIN Courses c ON [Link] = [Link];
4. Find out how many students are enrolled in each course.
SELECT [Link], COUNT(*) AS StudentCount
FROM Students s
JOIN Courses c ON [Link] = [Link]
GROUP BY [Link];
5. Display the name of the student who has the highest marks in the course
"Data Structures".
SELECT [Link]
FROM Students s
JOIN Courses c ON [Link] = [Link]
WHERE [Link] = 'Data Structures'
ORDER BY [Link] DESC
LIMIT 1;
6. List the courses along with the number of male students enrolled in each
course.
SELECT [Link], COUNT(*) AS MaleStudents
FROM Students s
JOIN Courses c ON [Link] = [Link]
WHERE [Link] = 'M'
GROUP BY [Link];
7. Find the average marks obtained by female students.
SELECT AVG(Marks) AS AvgMarks
FROM Students
WHERE Gender = 'F';
8. Retrieve the names of students who are older than 21 and are enrolled in
courses taught by Dr. Mehta.
SELECT [Link]
FROM Students s
JOIN Courses c ON [Link] = [Link]
WHERE [Link] > 21 AND [Link] = 'Dr. Mehta';
9. Find the course names where the average marks of students are greater
than 85.
SELECT [Link]
FROM Students s
JOIN Courses c ON [Link] = [Link]
GROUP BY [Link]
HAVING AVG([Link]) > 85;
10. Display the names and marks of students who scored less than the
average marks in their respective courses.
SELECT [Link], [Link]
FROM Students s
JOIN Courses c ON [Link] = [Link]
WHERE [Link] < (SELECT AVG(Marks) FROM Students WHERE CourseID =
[Link]);
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 [Link], [Link], [Link]
FROM Students s
JOIN Courses c ON [Link] = [Link]
WHERE [Link] = (SELECT MAX(Marks)
FROM Students
WHERE Marks < (SELECT MAX(Marks)
FROM Students
WHERE CourseID = [Link])
AND CourseID = [Link]);
13. Display the name of the student and their course who scored the lowest
marks in each course.
SELECT [Link], [Link], [Link]
FROM Students s
JOIN Courses c ON [Link] = [Link]
WHERE [Link] = (SELECT MIN(Marks) FROM Students WHERE CourseID =
[Link]);
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 [Link] AS Student1, [Link] AS Student2, [Link]
FROM Students s1
JOIN Students s2 ON [Link] = [Link] AND [Link] <
[Link]
JOIN Courses c ON [Link] = [Link];
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 [Link], COUNT(*) AS StudentCount
FROM Students s
JOIN Courses c ON [Link] = [Link]
GROUP BY [Link]
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 [Link]
FROM Students s
WHERE [Link] = 'F'
AND [Link] 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.
SELECT [Link], MAX([Link]) - MIN([Link]) AS MarkDifference
FROM Students s
JOIN Courses c ON [Link] = [Link]
GROUP BY [Link];
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 [Link], COUNT(*) AS StudentCount
FROM Students s
JOIN Courses c ON [Link] = [Link]
GROUP BY [Link]
HAVING COUNT(*) > 1;