You don't have any condition that states how the student would be associated with the student group. So, the EXISTS
always returns true, but you want to see the rows where the EXISTS
returns false.
You can add the WHERE
in the sub query to associate the student with the group.
SELECT student.lastname
FROM student INNER JOIN enrolled ON enrolled.studentid = student.sid
WHERE NOT exists(
SELECT *
FROM studentgroup INNER JOIN memberof ON memberof.groupid = studentgroup.gid
WHERE student.sid = memberof.studentid
)