The ON operator
Logically, every SQL query is executed in the following order:
FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY
You can read about this further from the official documentation from MSDN. SELECT (Transact-SQL)
This means the on
predicate relates to the cardinal matches between tables, while the WHERE
clause filters the results.
Cardinal means 1:N, or the number of matches to a side. In your example, ON A.CUSTOMER_ID = B.CUSTOMER_ID
will return a row for every matching set from the source table
LEFT
and RIGHT
refer to which side is the source table. By default, the
left is considered the source table.
So if table A has 3 rows where ID = 3
, then even if table B has only one ID of 3, you will return 3 rows; each row in Table A is treated separately.
A good join only uses the number of columns required to return a unique join, so that unwanted repeating values are not returned. Even if you meant to use a CROSS JOIN
, you still need to make sure to use unique matching sets for your purpose.
Relationally, what does the joins mean?
This is the real question: what do the tables represent and how do they answer a question?
Relational means value, information, a question or query answered.
When you know what the batch or proc does or what its purpose is for the script(s), identifying silly queries becomes easier.
CONCLUSION
ON ID = ID
- selects matching rows.
ON ID <> ID
- returns every nonmatching row for every row in the source table. Essentially a cross join minus the actual join rows.
Good practice is to use the ON to identify unique rows that match and the WHERE clause to filter this result on the side of the source table.