Skip to content
Advertisement

Joining two select statements together with outer join

My query on SQL Server:

I am trying to join these two queries and am having trouble. Is the outer join statement in the wrong place? Can someone please help me fix this code?

EDIT: outer join is not valid syntax here, so I am using left join. The query still returns an error:

Advertisement

Answer

Summary

outer join is not valid in SQL Server/t-sql I believe. It should either be left outer join, right outer join or full outer join.

In your situation, I suspect you want it to be a left outer join.

Explanation/longer version

In left and right outer joins, the ‘left’ and ‘right’ refer to the tables/etc literally to the left and right on the join (e.g., before and after the join, respectively).

  • In a left outer join, it takes all the values from the table on the left (first table) and any matching rows in the table on the right
  • In a right outer join, it takes all the values from the table on the right (second table) and any matching rows in the table on the left

A full outer join gets all rows from both tables, and matches them when they can.

Here is an example

Given you have WHERE s.learners_id in (...) in your WHERE clause, it implies you do not want rows where s.learners would be NULL.

  • If you had a right outer join, that WHERE requirement will effectively turn the right outer join into an inner join (as it would exclude all rows where s.learners_id is NULL).
  • If you had a full outer join, that WHERE requirement would effectively turn the full outer join into a left outer join – along similar logic lines.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement