SQL Server Only Return Student Name If He Is Not Studying Math Across Multiple Rows [closed]

dy2hfwbg  于 2023-05-28  发布在  其他
关注(0)|答案(2)|浏览(159)

Closed. This question is not reproducible or was caused by typos . It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 4 days ago.
Improve this question

I have a table called student.

student_namesubject
Amath
Aenglish
Bmath
Cenglish

Now I want a sql query which will only output student name who are not studying math. In the case the output should be student c.

SELECT student_name, subject
FROM students
WHERE student_name NOT IN (
    SELECT student_name
    FROM students
    WHERE subject = 'Math'
);

In this solution it will pick student a from row 1.

cs7cruho

cs7cruho1#

This code worked for me

SELECT student_name, subject
FROM students
WHERE student_name NOT IN (
    SELECT student_name
    FROM students
    WHERE subject = 'Math'
);
ffx8fchx

ffx8fchx2#

select student_name from students
except
select student_name from students where subject = 'Math'

or

select distinct student_name
from students s1
where not exists (
    select 1 from students s2
    where s2.student_name = s1.student_name
    and s2.subject = 'Math'
)

相关问题