I have two tables, employees and unionMembers. The people in unionMembers are a small subset of the people in employees. I want to do a SELECT query using a LEFT JOIN like so:
SELECT wage, unionDues
FROM employees
LEFT JOIN unionMembers ON employees.ID = unionMembers.ID
WHERE employees.ID = 'ID001';
Does it matter if I do ON employees.ID = unionMembers.ID
or reverse the order and do ON unionMembers.ID = employees.ID
?
Let's say I have 100 employees and 5 of them are union members. When doing the LEFT JOIN, ideally it would have to do 5 comparisons instead of a 100. Would the order of the equality test make any difference?
EXPLAIN SELECT
for both options and compare the resulting query plans. If the plans end up being completely identical, the answer should be obvious. Feel free to post your own answer if you manage to reach a conclusion.JOIN
, always qualify the column names. In this example, ifunionDues
is inunionMembers
, then theIN (SELECT ...)
approach won't work. If it is inemployees
, then why have theLEFT JOIN
at all??