Home > Software engineering >  Left join vs. inner join not returning expected records
Left join vs. inner join not returning expected records

Time:11-18

I am trying to find all of the most recent records (attempt_date) for a given "course" for each user. The query below returns the correct date for each user, unless the user doesn't have an attempt_date. In that case, the query does not return a row with the user at all.

If I change the inner joins on gradebook_grade and attempt to left join, it returns all enrolled users, but the query then returns null values if a null value exists for the any submission in the "course" rather than just the most recent attempt_date. Query here (forgive the weird naming conventions for term in the where clause, I did not choose those):

select distinct on (cu.pk1)
cu.pk1
,cm.course_id
,a.attempt_date
from course_users cu
    inner join course_main cm on cm.pk1 = cu.crsmain_pk1
    inner join course_term ct on ct.crsmain_pk1 = cm.pk1 /* through table */
    inner join term t on t.pk1 = ct.term_pk1
    inner join gradebook_grade gg on gg.course_users_pk1 = cu.pk1
    inner join attempt a on a.gradebook_grade_pk1 = gg.pk1
where t.name like '           
  • Related