Class
- ClassId (PK)
- ClassName
- ClassCapacity
Batch
- BatchId (PK)
- BatchName
- Size
- Status {NotStarted, Enrolled, Completed}
BatchClass
- BatchClassId (PK)
- BatchId (FK)
- ClassId (FK)
- NoOfStudents
Relationship
- A class has maximum capacity, therefore a batch can have many classes, but at a given time a class can be allocated only for one batch that status is = Enrolled (This is to be validated from the application end)
I want to get all the classes that are not currently allocated to a batch that status is not equal to Enrolled
This is what I tried,
SELECT C.*
FROM Class C
LEFT JOIN (
Batch B
INNER JOIN BatchClass BC
ON B.BatchId = BC.BatchId
) ON C.ClassId = BC.ClassId
WHERE B.Status <> "Enrolled";
Even though when I try WHERE B.Status = “Enrolled” it gives all the classes that have a enrolled batch. What I want is the Opposite which didn’t work for me from above SQL.
I’m not sure is it something wrong in my design or SQL statement. Please help. Thank you in advance.
This should work fine:SQL Fiddle Demo
Update 1
Try this instead:
Updated SQL Fiddle Demo