Given a table containing class and students, need to find the diff in students
Use, for example

SELECT student_id
FROM class_students
GROUP BY student_id
HAVING SUM(class_id = 1) &gt; 0
   AND SUM(class_id != 1) = 0


More informative is

SELECT student_id, GROUP_CONCAT(DISTINCT class_id) classes_in
FROM class_students
GROUP BY student_id
HAVING COUNT(DISTINCT class_id) &lt; ( SELECT COUNT(DISTINCT class_id)
                                    FROM class_students )


PS. Recommendation - define (class_id, student_id) (or in backward fields order) as unique/primary index - it will forbid duplicated records.