I am a beginner at SQL. I have a single table called enrolment where I have users (user_id) who can have either single or multiple courses which are represented using course_id.
I want to find the users who are only enrolled into the course 10, have no due fees (due_fee) and nothing else.
This is my attempt at this problem but it is not working:
SELECT user_id FROM student_enrollment WHERE course_id = 10 and due_fee = 0.00
This query is selecting the users who have a course_id of 10 but how do I filter this so that the users who have both 10 and other course_id’s are removed? Then the end result would only be the users with course_id 10.
3
Answers
You could phrase this using exists logic:
Another approach, using aggregation:
A simple way is to use
EXCEPT
: Find all users that take class #10 except those who also take another class.https://dev.mysql.com/doc/refman/8.0/en/except.html
Using conditional aggregation and filter: