Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL inner joining to left joined table

So this might be more of a theoretical question about how joins in MySQL work, but I'd love some guidance.

Let's say I have three tables, table a, b and c, where table a and b are fact tables and table c is table b's dimension table. If I want to left join table b to table a (I want to keep all of the contents of table a, but also want matching contents in table b), can I still inner join table c to table b even table b is left joined? Or do I have to left join table c to table b? Or would both of these for all intents and purposes produce the same result?

select a.column, c.name
from tablea a
left join tableb b on a.id = b.id
inner join (?) tablec c on b.name_id = c.name
like image 596
ben890 Avatar asked Feb 18 '15 17:02

ben890


People also ask

When inner join when left join?

You'll use INNER JOIN when you want to return only records having pair on both sides, and you'll use LEFT JOIN when you need all records from the “left” table, no matter if they have pair in the “right” table or not.

Is there a left inner join in SQL?

An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. There are different types of joins available in SQL: INNER JOIN: returns rows when there is a match in both tables. LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.

How do I join two tables with left join in SQL?

SELECT column names FROM table1 LEFT JOIN table2 ON table1. matching_column = table2. matching_column; Note: For example, if you have a left table with 10 rows, you are guaranteed to have at least 10 rows after applying join operation on two tables.

Is it possible for left join and inner join to produce the same results?

The reason why LEFT JOIN and INNER JOIN results are the same is because all the records of table branch has at least one match on table user_mast . The main difference between INNER JOIN and LEFT JOIN is that LEFT JOIN still displays the records on the the LEFT side even if they have no match on the RIGHT side table.


2 Answers

MySQL supports syntax that allows you to achieve what you want:

select a.column, c.name
from
  tablea a
  left join
    tableb b
    inner join tablec c on b.name_id = c.name
   on a.id = b.id
;

In this case tables tableb and tablec are joined first, then the result of their join is outer-joined to tablea.

The final result set, however, would be same as with @simon at rcl's solution.

like image 100
Andriy M Avatar answered Oct 06 '22 11:10

Andriy M


In this case if there is no tablec entry for a tableb, then the whole join will fail and the tablea row will not be included. To include the tablsa entry you would need to make the join to tablc a left join:

select a.column, c.name
from tablea a
left join tableb b on a.id = b.id
left join tablec c on b.name_id = c.name

That will get you every tablea row even when there is no matching tableb row, and also every tablea and tableb when there is no tablec row.

like image 27
simon at rcl Avatar answered Oct 06 '22 11:10

simon at rcl