1

I have multiple SQL queries that look similar where one uses JOIN and another LEFT OUTER JOIN. I played around with SQL and found that it the same results are returned. The codebase uses JOIN and LEFT OUTER JOIN interchangeably. While LEFT JOIN seems to be interchangeable with LEFT OUTER JOIN, I cannot I cannot seem to find any information about only JOIN. Is this good practice?

Ex Query1 using JOIN

SQL
SELECT 
   id,
   name 
FROM 
   u_users customers
JOIN 
   t_orders orders
ON orders.status=='PAYMENT PENDING'

Ex. Query2 using LEFT OUTER JOIN

SQL
SELECT 
   id,
   name 
FROM 
   u_users customers
LEFT OUTER JOIN 
   t_orders orders
ON orders.status=='PAYMENT PENDING'
4

4 Answers 4

4

As previously noted above:

JOIN is synonym of INNER JOIN. It's definitively different from all types of OUTER JOIN

So the question is "When should I use an outer join?"

Here's a good article, with several great diagrams:

https://www.sqlshack.com/sql-outer-join-overview-and-examples/

enter image description here

The short answer your your question is:

  • Prefer JOIN (aka "INNER JOIN") to link two related tables. In practice, you'll use INNER JOIN most of the time.
  • INNER JOIN is the intersection of the two tables. It's represented by the "green" section in the middle of the Venn diagram above.
  • Use an "Outer Join" when you want the left, right or both outer regions.
  • In your example, the result set happens to be the same: the two expressions happen to be equivalent.
  • ALSO: be sure to familiarize yourself with "Show Plan" (or equivalent) for your RDBMS: https://www.sqlshack.com/execution-plans-in-sql-server/
1
  • Such pseudo-Venn diagrams are misleading & unhelpful & a correct legend is complex. Eg tables are bags not sets & the circles are not the input tables but they are labelled so. Also in this case the text on the sub-circle areas is not clear. See my comment on another answer.
    – philipxy
    Commented Jun 18, 2021 at 1:07
1

First the theory:

A join is a subset of the left join (all other things equal). Under some circumstances they are identical

The difference is that the left join will include all the tuples in the left hand side relation (even if they don't match the join predicate), while the join will only include the tuples of the left hand side that match the predicate.

For instance assume we have to relations R and S.

Say we have to do R JOIN S (and R LEFT JOIN S) on some predicate p

J = R JOIN S on (p)

Now, identify the tuples of R that are not in J.

Finally, add those tuples to J (padding any attribute in J not in R with null)

This result is the left join:

R LEFT JOIN S (p)

So when all the tuples of the left hand side of the relation are in the JOIN, this result will be identical to the Left Join.

back to you problem:

Your JOIN is very likely to include all the tuples from Users. So the query is the same if you use JOIN or LEFT JOIN.

0

The two are exactly equivalent, because the WHERE clause turns the LEFT JOIN into an INNER JOIN.

When filtering on all but the first table in a LEFT JOIN, the condition should usually be in the ON clause. Presumably, you also have a valid join condition, connecting the two tables:

SELEC id, name 
FROM u_users u LEFT JOIN 
     t_orders o
     ON o.user_id = u.user_id AND o.status = 'PAYMENT PENDING';

This version differs from the INNER JOIN version, because this version returns all users even those with no pending payments.

2
  • Does the where always change left join into an inner join? According to other comments the resultset happens to be the same due to my example. But in my codebase the tables are much larger/complex with queries and so it is difficult to justify if this is the correct answer Commented Jun 23, 2021 at 15:16
  • @DawnAxolotl . . . No always. But almost any comparison on all but the first table does. Commented Jun 23, 2021 at 18:00
-1

Both are the same, there is no difference here.

You need to use the ON clause when using Join. It can match any data between two tables when you don't use the ON clause. This can cause performance issue as well as map unwanted data.

If you want to see the differences you can use "execution plans".

for example, I used the Microsoft AdventureWorks database for the example.

LEFT OUTER JOIN :

Outer Join

LEFT JOIN : Just LEFT JOIN

If you use the ON clause as you wrote, there is a possibility of looping. Example "execution plans" is below.

not mapping

All Join Practices

You can access the correct mapping and data using the ON clause complement.

select 
   id,
   name 
from 
   u_users customers
left outer join 
   t_orders orders on customers.id = orders.userid
where orders.status=='payment pending'
2
  • 1
    Those psuedo-Venn diagrams are commonly passed on like a virus, but they are not helpful. What do the elements of the circles represent? What are the circles? You don't say. They aren't A & B. And they aren't SQL tables because those are bags not sets. It is extremely difficult to give a correct legend. Your particular versions don't handle the general case of the operators. Venn Diagram for Natural Join PS The issues in this question are decades-old duplicate questions. How to Answer help center
    – philipxy
    Commented Jun 17, 2021 at 22:45
  • 1
    LEFT JOIN returns INNER JOIN rows UNION ALL unmatched left table rows extended by NULLs. Always know what INNER JOIN you want as part of an OUTER JOIN. After a LEFT JOIN a WHERE, INNER JOIN or HAVING that requires a right [sic] table column to be not NULL removes any rows with introduced NULLs, ie leaves only INNER JOIN rows, ie "turns OUTER JOIN into INNER JOIN". You have that. PS This is a faq.
    – philipxy
    Commented Jun 17, 2021 at 22:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.