Databaseprint 69
Databaseprint 69
1. Retrieve the average grade for all students across all courses.
sql
Copy code
SELECT AVG(Grade) AS AverageGrade
FROM Enrollment;
2. Find all students who are enrolled in more than three courses in a single
semester.
sql
Copy code
SELECT StudentID, Semester
FROM Enrollment
GROUP BY StudentID, Semester
HAVING COUNT(CourseID) > 3;
3. List the names of students who have never enrolled in any course.
sql
Copy code
SELECT Name
FROM Student
WHERE StudentID NOT IN (
SELECT DISTINCT StudentID
FROM Enrollment
);
6. List the courses that are taught by more than one instructor in the same
semester.
sql
Copy code
SELECT CourseID, Semester
FROM Teaches
GROUP BY CourseID, Semester
HAVING COUNT(DISTINCT InstructorID) > 1;
8. Find the students who have enrolled in both "Database Systems" and
"Operating Systems."
sql
Copy code
SELECT S.Name
FROM Student S
JOIN Enrollment E1 ON S.StudentID = E1.StudentID
JOIN Course C1 ON E1.CourseID = C1.CourseID
JOIN Enrollment E2 ON S.StudentID = E2.StudentID
JOIN Course C2 ON E2.CourseID = C2.CourseID
WHERE C1.Name = 'Database Systems' AND C2.Name = 'Operating Systems';
9. Retrieve the details of students majoring in "Computer Science" who have
earned an average grade greater than 3.5 across all their courses.
sql
Copy code
SELECT S.*
FROM Student S
JOIN Enrollment E ON S.StudentID = E.StudentID
WHERE S.Major = 'Computer Science'
GROUP BY S.StudentID, S.Name, S.Email, S.Major
HAVING AVG(E.Grade) > 3.5;
10. List the names of instructors who have not taught any courses during the
Fall 2024 semester.
sql
Copy code
SELECT Name
FROM Instructor
WHERE InstructorID NOT IN (
SELECT DISTINCT InstructorID
FROM Teaches
WHERE Semester = 'Fall 2024'
);
11. Retrieve the names of students enrolled in the most number of courses
during the Spring 2024 semester.
sql
Copy code
SELECT S.Name
FROM Student S
JOIN Enrollment E ON S.StudentID = E.StudentID
WHERE E.Semester = 'Spring 2024'
GROUP BY S.StudentID, S.Name
HAVING COUNT(E.CourseID) = (
SELECT MAX(CourseCount)
FROM (
SELECT COUNT(CourseID) AS CourseCount
FROM Enrollment
WHERE Semester = 'Spring 2024'
GROUP BY StudentID
) AS Temp
);
12. Find the details of the course with the highest number of students enrolled
in the Fall 2024 semester.
sql
Copy code
SELECT C.*
FROM Course C
JOIN Enrollment E ON C.CourseID = E.CourseID
WHERE E.Semester = 'Fall 2024'
GROUP BY C.CourseID, C.Name, C.Credits, C.Department
ORDER BY COUNT(E.StudentID) DESC
LIMIT 1;
13. Retrieve the names of students who have taken at least one course from
the "Mathematics" department.
sql
Copy code
SELECT DISTINCT S.Name
FROM Student S
JOIN Enrollment E ON S.StudentID = E.StudentID
JOIN Course C ON E.CourseID = C.CourseID
WHERE C.Department = 'Mathematics';
15. Retrieve the details of courses that are worth more than the average
number of credits.
sql
Copy code
SELECT *
FROM Course
WHERE Credits > (
SELECT AVG(Credits)
FROM Course
);
16. Find the names of students who have not achieved a grade lower than "B"
in any course.
sql
Copy code
SELECT DISTINCT S.Name
FROM Student S
JOIN Enrollment E ON S.StudentID = E.StudentID
WHERE E.Grade NOT IN ('C', 'D', 'F')
GROUP BY S.StudentID, S.Name
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM Enrollment E2
WHERE E2.StudentID = S.StudentID
);
17. Retrieve the details of instructors who have taught courses only in the
"Engineering" department.
sql
Copy code
SELECT I.*
FROM Instructor I
JOIN Teaches T ON I.InstructorID = T.InstructorID
JOIN Course C ON T.CourseID = C.CourseID
GROUP BY I.InstructorID, I.Name, I.Email, I.Department
HAVING COUNT(DISTINCT C.Department) = 1 AND MAX(C.Department) =
'Engineering';
18. List the courses that have been offered in every semester.
sql
Copy code
SELECT C.CourseID, C.Name
FROM Course C
JOIN Teaches T ON C.CourseID = T.CourseID
GROUP BY C.CourseID, C.Name
HAVING COUNT(DISTINCT T.Semester) = (
SELECT COUNT(DISTINCT Semester)
FROM Teaches
);
19. Retrieve the names of students who have been taught by "Dr. Alice
Green" in any semester.
sql
Copy code
SELECT DISTINCT S.Name
FROM Student S
JOIN Enrollment E ON S.StudentID = E.StudentID
JOIN Teaches T ON E.CourseID = T.CourseID
JOIN Instructor I ON T.InstructorID = I.InstructorID
WHERE I.Name = 'Dr. Alice Green';
20. Find the department with the highest average grade for its courses.
sql
Copy code
SELECT C.Department
FROM Course C
JOIN Enrollment E ON C.CourseID = E.CourseID
GROUP BY C.Department
ORDER BY AVG(E.Grade) DESC
LIMIT 1;
Schema:
1. Retrieve the names of students enrolled in the course named "Database Systems" in
Fall 2024.
Answer:
πStudent.Name(σCourse.Name=′DatabaseSystems′∧Enrollment.Semester=′Fall2024′(
Student⋈Enrollment⋈Course))
4. List all students taught by the instructor named "Dr. John Smith."
Answer:
πStudent.Name(σInstructor.Name=′Dr.JohnSmith′(Student⋈Enrollment⋈Teaches⋈I
nstructor))
5. Retrieve the details of students who have taken more than three courses in a single
semester.
Answer:
πStudentID,Name,Email,Major(σcount(CourseID)>3(γStudentID,Semester;count(Cou
rseID)(Enrollment)⋈Student))
7. List all students with a grade of "A" in any course during the Spring 2024 semester.
Answer:
πName(σGrade=′A′∧Semester=′Spring2024′(Student⋈Enrollment))
9. Retrieve the details of courses not taught by any instructor in the Spring 2024
semester.
Answer:
Course−πCourseID,Name,Credits,Department(σTeaches.Semester=′Spring2024′(Cour
se⋈Teaches))
10. List all instructors who have taught the course named "Machine Learning."
Answer:
πInstructor.Name(σCourse.Name=′MachineLearning′(Instructor⋈Teaches⋈Course))
11. Retrieve the names of students majoring in "Data Science" who are enrolled in any
"Computer Science" course.
Answer:
πStudent.Name(σStudent.Major=′DataScience′∧Course.Department=′ComputerScien
ce′(Student⋈Enrollment⋈Course))
12. Find the average grade for the course named "Introduction to Programming."
Answer:
πavg(Grade)(γavg(Grade)(σCourse.Name=′IntroductiontoProgramming′(Enrollment
⋈Course)))
13. Retrieve the details of students who have not enrolled in any course during the Spring
2024 semester.
Answer:
Student−πStudentID,Name,Email,Major(σEnrollment.Semester=′Spring2024′(Student
⋈Enrollment))
14. List all courses with at least five students enrolled in the Fall 2024 semester.
Answer:
πCourseID,Name(σcount(StudentID)≥5(γCourseID;count(StudentID)(σEnrollment.Se
mester=′Fall2024′(Enrollment))))
15. Retrieve the details of instructors who have not taught any courses during the past
year.
Answer:
Instructor−πInstructorID,Name,Email,Department(σTeaches.Semester∈{′Spring2024′
,′Fall2024′}(Instructor⋈Teaches))