Skip to content
Advertisement

Understanding where clause vs join

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'

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement