DB Task (Nested Query)
DB Task (Nested Query)
Here are the SQL QUERY: queries for each of the tasks:
1. Names of students whose GPA is above the average GPA of all students:
SQL Query:
SELECT Name FROM students WHERE GPA > (SELECT AVG(GPA) FROM students);
2. Subjects taught by teachers whose monthly salary is higher than the average
monthly salary of all teachers:
SQL Query:
SQL Query:
4. Names of students who are enrolled in classes with a grade level higher than
the average grade level of all classes:
SQL Query:
5. Names of teachers who teach classes with more students than the average
number of students in all classes:
SQL Query:
SELECT DISTINCT t.Name FROM teachers AS t JOIN classes AS c ON t.TeacherID =
c.TeacherID GROUP BY t.TeacherID HAVING COUNT(c.StudentID) > (SELECT
AVG(StudentCount) FROM (SELECT COUNT(StudentID) AS StudentCount FROM
classes GROUP BY TeacherID) AS avg_student_count);
SQL Query:
SQL Query:
8. Names of students who have a GPA higher than the GPA of any student in
class 12:
SQL Query:
SELECT s.Name FROM students AS s WHERE s.GPA > (SELECT MAX(GPA) FROM
students WHERE ClassID = 12);
SQL Query:
SQL Query:
SELECT COUNT(s.StudentID) FROM students AS s JOIN classes AS c ON s.ClassID =
c.ClassID JOIN teachers AS t ON c.TeacherID = t.TeacherID WHERE t.Subject IN
('Mathematics', 'History');
11. Names of students who have a GPA higher than the GPA of all students in
class 10:
SQL Query:
SELECT s.Name FROM students AS s WHERE s.GPA > ALL (SELECT GPA FROM students
WHERE ClassID = 10);
12. Names of students whose GPA is above the average GPA of students in
classes with more than 25 students:
SQL Query:
13. Number of students in classes where the teacher's subject is not English:
SQL Query:
14. Names of students who are enrolled in classes with a grade level higher than
the grade level of any class taught by a teacher with a monthly salary less
than $2500:
SQL Query:
15. Subjects taught by teachers whose monthly salary is greater than the
monthly salary of any teacher teaching Mathematics or History:
SQL Query:
SELECT DISTINCT Subject FROM teachers WHERE MonthlySalary > (SELECT
MAX(MonthlySalary) FROM teachers WHERE Subject IN ('Mathematics', 'History'));
END