Skip to content
Advertisement

Flag if there is a JOIN – SQL

Two tables with same field names >> student.id in student table (a) and student.id in record table (b)

If student.id from student table is also in record table (join on a.student.id = b.student.id) then say “Yes” else “No” as “match”

Advertisement

Answer

If there are no duplicates, then you can use a left join:

select s.*, (case when r.student_id is null then 'No' else 'Yes' end)
from students s left join
     records r
     on r.student_id = s.id;

If there can be duplicates, then remove the duplicates before joining:

select s.*, (case when r.student_id is null then 'No' else 'Yes' end)
from students s left join
     (select distinct r.student_id
      from records r
     ) r
     on r.student_id = s.id;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement