How do I decide which is the left table?

Hi,

I am currently studying SQL and while I understand how joins work and that the left table is the table in the FROM statement, I'm still not clear on how you decide which table to use as the left table. Are there factors that should be considered to determine which table to use as the left table or is it just a case of it's up to you which one you want to use as the left table? I can't find anything from trying to Google an answer to this.

Many thanks,

Mark

For an INNER JOIN, the order you list tables in the FROM clause won't matter.

If you need a LEFT JOIN, you'd list the table whose rows you always want returned to be listed first, or LEFT-most in the FROM clause. The LEFT JOIN will still return rows in the leftmost table, even if the logical condition in the join doesn't find a match in the right table.

1 Like

It is very important to understand which table is the left table. For example: if you have a table employees and a table laptops and it is a 1:1 relationship between the two tables. So 1 employee can only have 1 tablet and 1 tablet can be owned by 1 employee.

If you select the table employee and left join the tables you get all employees including the employees without a laptop. You won't see all the tablets, you will miss the tablets without employees.

If you select the table tablets and left join the table employee you get all tablets including the tablets without a employee. You won't see all the employees, you will miss the employees without a tablet.

It is very important to understand this. SQL is easy to learn but hard to understand. For example, when you put a field of the left joined table in the WHERE statement it can have impact. You should try to create a stament with a few rows and try to understand the results you expect and what the actuall results are.

3 Likes

Thanks, Robert, Rogier, makes complete sense now. Sorry if this was a bit of a thicko question! I am going to run some examples to see the different outputs. Thanks again!!!