2

I have two tables, storing candidates and their grades. A simple join can get all the candidates and their grades, by matching IDs etc.

e.g:

Alan    C
George  A

etc.. However:

Nina

The grade for Nina is null because she did not take the exam.

How can I get output like the following?

Alan   C
George A
Nina

So still get Nina, even though she did not take the exam. I've tried null checks and then performing something, etc.

Thanks

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
blade4
  • 21
  • 2

3 Answers3

7

You want to use a left join.

select c.name, coalesce(g.grade,'')
    from candidate c
        left join grade g
            on c.candidate_id = g.candidate_id
Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235
1

You need to use a left join. For example:

 select c.name, g.grade
 from candidates c
 left join grades g on g.candidateid=c.id

A left join always lists all rows on the "left" side of the join - in this case, candidates. On rows where the candidate has no entry in the grades table, all columns that are filled from that table will be null.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
1

You need to use a left join for that.

steve
  • 849
  • 2
  • 9
  • 15