When should I use INNER JOIN instead of LEFT JOIN?

Question

When should I use INNER JOIN vs LEFT JOIN?

Answer

Depending on how we want to select the combined data, it can determine whether to use an INNER JOIN or a LEFT JOIN.

Generally, we use INNER JOIN when we want to select only rows that match an ON condition. If no rows match the ON condition, then it will not return any results. This can be somewhat stricter than using a LEFT JOIN.

We use a LEFT JOIN when we want every row from the first table, regardless of whether there is a matching row from the second table. This is similar to saying,
“Return all the data from the first table no matter what. If there are any matches with the second table, provide that information as well, but if not, just fill the missing data with NULL values.”

In a way, LEFT JOIN is less strict than INNER JOIN. Furthermore, the results of a LEFT JOIN will actually include all results that an INNER JOIN would have provided for the same given condition.

26 Likes

For a real world example:

We created/maintain a database at my work for trading purposes. We maintain a fund master list and each day receive a file with fund level cash flows. We have our queries set up to do a LEFT JOIN, joining all records of the fund master list to the cash flow file regardless of whether there is a match. The end result is we can view a report with all funds and their daily cash flow, and the ones that don’t have cash flow today will still show up but with a null value. This is helpful to know as too many portfolios with null values can be indicative of problems with the cash flow file.

103 Likes

This is more of an observation, in this exercise the second value in C3 is also deleted in the graphic. I’m guessing that is a bug? If not, what am I missing in this exercise?

1 Like

I think the idea behind this is that the row is occupied by the “left” table data, which has no match in the other table, giving null values as result

1 Like

If you are familiar with set theory (Mathematics) Then, INNER JOIN is more like (A intersection B)
and Left Join is {A union (A intersection B)}

22 Likes

This is more a limitation of the visualization used but not a bug. It supposes E in C2 doesn’t have a matching data in the left table and similarly that W doesn’t exist in the right table.

Because we are using a LEFT JOIN, the data on the left is kept in the table with no value in C3 while the data from the right table will not be displayed at all.

3 Likes

Just sharing an example from my work as well–we administer employee wellness programs and send program results through mail, combining program results and address information. If address info is NULL, we still want them in the query, so that we have their results. (We individually seal those without addresses and send the batch to HR to ensure delivery.) That’s a LEFT JOIN.

When I compile a list of emails for a program’s communication collateral for one client, I need to join tables with company associations and emails. We won’t be emailing NULL email addresses, so they can be omitted from results. JOIN works there.

23 Likes

thanks for real world example, sir

from my understanding, left join is like vlookup formula in excel, and inner join removes all the N/As.

8 Likes

Thanks for explaining with a real life example.

Thankyou for explaining with a Practical Life Example.

True, that’s like saying if we have two sets A and B, then:

  1. LEFT JOIN is equivalent to A - B
  2. RIGHT JOIN is equivalent to B - A
  3. INNER JOIN is equivalent to A U B
2 Likes

What would the breakdown of this be then, because I have a hard time telling the difference

SELECT *
FROM newspaper
LEFT JOIN online
ON newspaper.id = online.id
WHERE online.id IS NULL;

When you LEFT JOIN online to newspaper, you are selecting those customers who have only a newspaper subscription and also those have both newspaper and online subscriptions, but not the online subscription alone. In the result, the WHERE clause you mentioned at the end selects only those results which have online.id as NULL, that means you will only see users who have subscribed to newspaper and not online. So, in the end, you get a result containing only those users who have subscribed to newspaper AND not subscribed to online.

This image may help you visualize joins as sets:
sql_joins

44 Likes

Thank you for this answer, but one minor addition. INNER JOIN is more like A INTERSECTION B.

2 Likes

This is very good ! Thanks for this

1 Like

So basically any LEFT, or RIGHT, JOIN is kind of irrelevant since you can always get the data if you search for ‘WHERE something IS NULL’.
This picture really helps answer the question that arises with the given exercise.

From my perspective this was the incongruency with having both lefts and rights. In the end they’re just a way to get things directly, without recurring to WHEREs and NULLs.

Thanks for your help mate (dsonigladiator).

Have a nice one.

More examples like this will illustrate the concepts better

THIS WAS A TASTY EXPLANATION :smiley:

THANKS!

2 Likes

Appreciate this. Thanks!