0

I've got this homework for database 'SCHOOL'

  • Table Student (StudentID, Name, Surname, etc..)\
  • Table Application (ApplicationID, StudentID, ClassID)\
  • Table Class (ClassID, ClassName, TeacherID)\
  • Table Teacher (TeacherID, Name, Surname, etc..)

There are some other tables and columns in the database but I don't think they're important to this query.

I need to get name of the teachers for whose classes no student signed up. Let's say there is no one signed up for Math class, and I need to get the name of the teacher for that Math class.

SELECT Teacher.Name, Teacher.Surname, Class.ClassName
FROM Teacher
INNER JOIN Class ON Class.TeacherID = Teacher.TeacherID
INNER JOIN Application ON Application.ClassID = Class.ClassID
INNER JOIN Student ON Student.StudentID = Application.StudentID
WHERE Application.PredmetID IS NULL

Help would be appreciated.

3
  • Hint - use "where not exists (...)" Commented Nov 8, 2020 at 14:41
  • Have a look at NOT EXISTS or a LEFT JOIN. Commented Nov 8, 2020 at 14:42
  • I recommend using table aliases. Commented Nov 9, 2020 at 1:56

2 Answers 2

1

I would recommend NOT EXISTS:

select t.name
from class c join
     teacher t
     on t.teacherid = c.teacherid
where not exists (select 1
                  from application a
                  where a.classid = c.classid
                 );
Sign up to request clarification or add additional context in comments.

Comments

1

I need to get name of the teachers for whose classes no student signed up

Select T.name
From Teacher T inner join [Class] C on T.TeacherId = C.TeacherId
Where C.ClassId not in (Select Distinct ClassId from Application)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.