I've been using this for years, so it is high time to understand it fully. Suppose a query like this:
SELECT
*
FROM a
LEFT JOIN b ON foo...
LEFT JOIN c ON bar...
The documentation tells us that
T1 { [INNER] | { LEFT | RIGHT | FULL } [OUTER] } JOIN T2 ON boolean_expression
LEFT OUTER JOIN
First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Thus, the joined table always has at least one row for each row in T1.
The question is simple: what is T1
in this case? Is it a
? Or is it a LEFT JOIN b ON foo
? (or, is it the same?)
The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.
Syntax For Left Join: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.
LEFT JOIN , also called LEFT OUTER JOIN , returns all records from the left (first) table and the matched records from the right (second) table.
The LEFT JOIN command returns all rows from the left table, and the matching rows from the right table. The result is NULL from the right side, if there is no match.
A FROM
clause parses the conditions from left to right (unless overridden by parentheses). So:
FROM a
LEFT JOIN b
ON foo...
LEFT JOIN c
ON bar...
is parsed as:
FROM (
a
LEFT JOIN b
ON foo...
)
LEFT JOIN c
ON bar...
This is explained in the documentation under the join-type
section of the FROM
clause:
Use parentheses if necessary to determine the order of nesting. In the absence of parentheses,
JOIN
s nest left-to-right. In any caseJOIN
binds more tightly than the commas separatingFROM
-list items.
As a consequence, a series of LEFT JOIN
s keeps all records in the first mentioned table. This is a convenience.
Note that the parsing of the FROM
clause is the same regardless of the join type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With