2

As far as I understand in a left outer join between two tables (say a & b) all the rows of the table on the left side of the join are retrieved regardless of the values in the rows on the right table. Then why do we need an 'ON' clause specifying a condition, something like this:

select * from a LEFT OUTER JOIN b on a.some_column1 = b.some_column2;

Why is there a need for the statement "a.some_column1 = b.some_column2".

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Chhaganlaal
  • 113
  • 2
  • 9

3 Answers3

5

A left join would return all the rows from table a, and for each row the matching row in table b, if it exists - if it doesn't, nulls would be returned instead of b's columns. The on clause defines how this matching is done.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

An on clause is required since you are "joining", and you need to tell which columns you want to join by. Otherwise you would use traditional from without any where condition to all possible row combinations. But you wanted a join, right?

Yeah, that's pretty much it is.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • 1
    "traditional `from` without any `where` condition" is still a join--"implicit join"--a cross join. – philipxy Jun 13 '18 at 21:45
1

As far as I understand in a left outer join between two tables (say a & b) all the rows of the table on the left side of the join are retrieved regardless of the values in the rows on the right table.

That is correct in the sense that it says something about what left join on returns, but it isn't a definition of what it returns. left join on returns inner join on rows plus (union all) unmatched left table rows extended by nulls.

inner join on returns the rows of cross join that satisfy the on condition--which could be any condition on the columns. cross join returns every combination of a row from the left table & a row from the right table.

What do you expect outer join without on to mean? In standard SQL outer & inner join have to have an on. inner join on a true condition is the same as cross join. Which has no unmatched left table rows. So if you want outer join with no on to mean outer join on a true condition then, since there are no unmatched rows in the inner join on that condition, the result is also just cross join. (MySQL allows inner join to be used without an on, but it just means cross join.)

philipxy
  • 14,867
  • 6
  • 39
  • 83