0% found this document useful (0 votes)
0 views4 pages

SQL Query Example-Solution

The document contains a series of SQL queries related to student and course data. It includes queries for retrieving student details, calculating averages, counting enrollments, and finding specific student performance metrics. Additionally, it addresses operations like deleting records and identifying courses with no students enrolled.

Uploaded by

Deepanshu Tyagi
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)
0 views4 pages

SQL Query Example-Solution

The document contains a series of SQL queries related to student and course data. It includes queries for retrieving student details, calculating averages, counting enrollments, and finding specific student performance metrics. Additionally, it addresses operations like deleting records and identifying courses with no students enrolled.

Uploaded by

Deepanshu Tyagi
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/ 4

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 s.Name, c.CourseName
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID;

4. Find out how many students are enrolled in each course.


SELECT c.CourseName, COUNT(*) AS StudentCount
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
GROUP BY c.CourseName;
5. Display the name of the student who has the highest marks in the course
"Data Structures".
SELECT s.Name
FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
WHERE c.CourseName = 'Data Structures'
ORDER BY s.Marks DESC
LIMIT 1;

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;

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

SELECT c.CourseName, MAX(s.Marks) - MIN(s.Marks) AS MarkDifference


FROM Students s
JOIN Courses c ON s.CourseID = c.CourseID
GROUP BY c.CourseName;

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;

You might also like