query for non existant rows

  • Please help me with the query.

    I have 2 tables Student and Classes

    student

    studentid name

    1 john

    2 steven

    3 paul

    classes

    studentid name subject

    1 john subject1

    2 steven subject2

    3 paul subject1

    3 paul subject2

    i want result as below

    studentid subject1 subject2

    1 VB NULL

    2 NULL C#

    3 VB C#

    I want to know whether this student is taking subject 2 or not. Please help, thanks alot!!

  • If there are few subjects like 2-3, check this:

    select

    s.studentId, c1.subject, c2.subject

    from student s

    left join classes c1

    on c1.studentId = s.studentId

    and c1.subject = 'subject1'

    left join classes c2

    on c2.studentId = s.studentId

    and c2.subject = 'subject2'

    ... otherwise if there are many-variable subjects then you will need to do a single INNER JOIN and then PIVOT the results.

  • Thanks a lot,it worked.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply