Hello,
I'm not a SQL Expert. I'm using SQL Server 2005 and I'm trying to figure out how to structure a query so that it can meet several requirements. I have two tables defined as follows:
Classroom
- ID
- DepartmentID
- TeacherName
- RoomNumber
Student
- ID
- Name
- ClassroomID
I'm trying to build a query that says, "give me the classrooms in department [x] OR department [y] that have more than 30 students AND give me the classrooms in department [w] OR department [z] that have more than 40 students. I'm confused on how to mix the ANDs and the ORs properly in my SQL. Currently, I am trying the following:
SELECT
c.RoomNumber,
c.TeacherName
FROM
Classroom c
WHERE
c.[DepartmentID]=5 AND (SELECT COUNT(*) FROM Student s WHERE s.ClassroomID=c.ID > 30) OR
c.[DepartmentID]=6 AND (SELECT COUNT(*) FROM Student s WHERE s.ClassroomID=c.ID > 30) OR
c.[DepartmentID]=7 AND (SELECT COUNT(*) FROM Student s WHERE s.ClassroomID=c.ID > 40) OR
c.[DepartmentID]=8 AND (SELECT COUNT(*) FROM Student s WHERE s.ClassroomID=c.ID > 40)
What am I doing wrong? Thank you!