0% found this document useful (0 votes)
15 views

DB Task (Nested Query)

The document contains 15 SQL queries related to retrieving student, teacher and class data from a school database. The queries include filtering on attributes like GPA, salary, subject, grade level and counts of students. Relations between students, classes, and teachers are joined in many of the queries.

Uploaded by

IRSHAD EDITS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DB Task (Nested Query)

The document contains 15 SQL queries related to retrieving student, teacher and class data from a school database. The queries include filtering on attributes like GPA, salary, subject, grade level and counts of students. Relations between students, classes, and teachers are joined in many of the queries.

Uploaded by

IRSHAD EDITS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 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:

SELECT DISTINCT Subject FROM teachers WHERE MonthlySalary > (SELECT


AVG(MonthlySalary) FROM teachers);

3. Number of students in classes taught by teachers who teach History or


English:

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
('History', 'English');

4. Names of students who are enrolled in classes with a grade level higher than
the average grade level of all classes:

SQL Query:

SELECT s.Name FROM students AS s JOIN classes AS c ON s.ClassID = c.ClassID


WHERE c.GradeLevel > (SELECT AVG(GradeLevel) FROM classes);

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);

6. GPA of students in classes where the teacher's subject is not Literature:

SQL Query:

SELECT s.GPA 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 != 'Literature';

7. Number of students in classes where the teacher's monthly salary is greater


than the average monthly salary of teachers teaching Mathematics:

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.MonthlySalary >
(SELECT AVG(MonthlySalary) FROM teachers WHERE Subject = 'Mathematics');

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);

9. Subjects taught by teachers whose monthly salary is greater than the


monthly salary of any teacher teaching History:

SQL Query:

SELECT DISTINCT Subject FROM teachers WHERE MonthlySalary > (SELECT


MAX(MonthlySalary) FROM teachers WHERE Subject = 'History');

10. Number of students in classes where the teacher's subject is Mathematics or


History:

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:

SELECT s.Name FROM students AS s JOIN classes AS c ON s.ClassID = c.ClassID


WHERE s.GPA > (SELECT AVG(GPA) FROM students WHERE ClassID IN (SELECT ClassID
FROM classes GROUP BY ClassID HAVING COUNT(StudentID) > 25));

13. Number of students in classes where the teacher's subject is not English:

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 !=
'English';

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:

SELECT s.Name FROM students AS s JOIN classes AS c ON s.ClassID = c.ClassID


WHERE c.GradeLevel > ANY (SELECT GradeLevel FROM classes WHERE TeacherID IN
(SELECT TeacherID FROM teachers WHERE MonthlySalary < 2500));

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

You might also like