-2

Can somebody explain the difference between below query examples.

select column1,column2 
from table1 
join table2 
  on table1.columna = table2.columna 
  and columna='1234'

vs

select column1,column2 
from table1 
join table2 
  on table1.columna = table2.columna 
where columna='1234'
user2378934
  • 41
  • 1
  • 10

1 Answers1

1

To answer the question in your comment, with the following tables:

table1 and table2

+---------+---------+    +---------+---------+ 
| column1 | columna |    | column2 | columna |
+---------+---------+    +---------+---------+
|      10 |    1234 |    |     100 |    1234 |
|      20 |    1234 |    |     200 |    5678 |
|      30 |    5678 |    +---------+---------+
|      40 |    9876 |
+---------+---------+

The inner join queries will produce:

+---------+---------+
| column1 | column2 |
+---------+---------+
|      10 |     100 |
|      20 |     100 |
+---------+---------+

An outer join query including the filter in the on clause will produce:

+---------+---------+
| column1 | column2 |
+---------+---------+
|      10 |     100 |
|      20 |     100 |
|      30 |         |
|      40 |         |
+---------+---------+

whereas an outer join query with the filter after the join will produce:

+---------+---------+
| column1 | column2 |
+---------+---------+
|      10 |     100 |
|      20 |     100 |
+---------+---------+

Note that since columna is in both tables, you have to indicate which columna the filter is associated to. I assumed table1's.

vc 74
  • 37,131
  • 7
  • 73
  • 89
  • Thanks for the elaborative answer, I spent some time to understand it yesterday and was able to make sense out of it. I really appreciate you taking the time out for explaining it and confirming my understanding as well. – user2378934 Jan 18 '19 at 18:29