Get the ID with only 1 term [closed]

I have a table with ID, section IDs, and terms. Student can take multiple sections in 1 term. Students registered in many terms.

ID StudentID Section ID Term
1 123456 41454 23sp
2 123456 41455 23sp
3 123456 51123 22fa
4 123456 79835 22fa
5 098765 41455 22sp
6 098765 51123 22sp
7 098765 79835 22sp
8 123987 41455 23sp
9 123987 51123 22fa
10 543987 41455 23sp
11 543987 79835 22fa

I want to get a student only have 1 same term with many sections. Example, 098765 has only 22sp term.

Thank you

  • “ID” usually means a unique key. If it’s not unique I would name it differently to avoid confusion.

    – 

  • 3

    select id from t group by id having count(distinct term) = 1?

    – 

  • Thank you @TheImpaler, The word (distinct term) solved this issue.

    – 

  • 1

    Mysql is not SQL server – please correct your tags

    – 

  • 4

    Please edit your post to correct the tags. MySQL and SQL Server are two totally different and distinct RDBMS systems, with many incompatibilities in functionality and syntax. Clearly you’re not using both at the same time. Tags have relevance and meaning here, and should be used properly. Tag spamming is a good way to get your questions downvoted and/or closed.

    – 

Leave a Comment