Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the SQL concepts LEFT OUTER JOIN and WHERE NOT EXISTS basically the same?

Tags:

Whats the difference between using a LEFT OUTER JOIN, rather than a sub-query that starts with a WHERE NOT EXISTS (...)?

like image 892
Steffan Harris Avatar asked Mar 19 '12 18:03

Steffan Harris


People also ask

Is left join and left outer join are same?

There really is no difference between a LEFT JOIN and a LEFT OUTER JOIN. Both versions of the syntax will produce the exact same result in PL/SQL. Some people do recommend including outer in a LEFT JOIN clause so it's clear that you're creating an outer join, but that's entirely optional.

What is the difference between left join with where clause and left join without where clause?

When you use a Left Outer join without an On or Where clause, there is no difference between the On and Where clause. Both produce the same result as in the following. First we see the result of the left join using neither an On nor a Where clause.

Which is better left outer or not exist?

It really depends, I just had two rewrite a query that was using not exists, and replaced not exists with left outer join with null check , yes it did perform much better. But always go for Not Exists, most of the time it will perform much better,and the intent is clearer when using Not Exists .

What does left outer join do in SQL?

A left outer join is a method of combining tables. The result includes unmatched rows from only the table that is specified before the LEFT OUTER JOIN clause. If you are joining two tables and want the result set to include unmatched rows from only one table, use a LEFT OUTER JOIN clause or a RIGHT OUTER JOIN clause.


2 Answers

No they are not the same thing, as they will not return the same rowset in the most simplistic use case.

The LEFT OUTER JOIN will return all rows from the left table, both where rows exist in the related table and where they does not. The WHERE NOT EXISTS() subquery will only return rows where the relationship is not met.

However, if you did a LEFT OUTER JOIN and looked for IS NULL on the foreign key column in the WHERE clause, you can make equivalent behavior to the WHERE NOT EXISTS.

For example this:

SELECT 
  t_main.*
FROM 
   t_main
   LEFT OUTER JOIN t_related ON t_main.id = t_related.id
/* IS NULL in the WHERE clause */
WHERE t_related.id IS NULL

Is equivalent to this:

SELECT
  t_main.*
FROM t_main 
WHERE 
  NOT EXISTS (
    SELECT t_related.id 
    FROM t_related 
    WHERE t_main.id = t_related.id
  )

But this one is not equivalent:

It will return rows from t_main both having and not having related rows in t_related.

SELECT 
  t_main.*
FROM
  t_main
  LEFT OUTER JOIN t_related ON t_main.id = t_related.id
/* WHERE clause does not exclude NULL foreign keys */

Note This does not speak to how the queries are compiled and executed, which differs as well -- this only addresses a comparison of the rowsets they return.

like image 97
Michael Berkowski Avatar answered Oct 02 '22 16:10

Michael Berkowski


As Michael already answered your question here is a quick sample to illustrate the difference:

Table A
Key     Data
1       somedata1
2       somedata2

Table B
Key     Data
1       data1

Left outer join:

SELECT *
FROM A
LEFT OUTER JOIN B
ON A.Key = B.Key

Result:

Key     Data        Key     Data
1       somedata1   1
2       somedata2   null    null

EXISTS use:

SELECT *
FROM A WHERE EXISTS ( SELECT B.Key FROM B WHERE A.Key = B.Key )

Not Exists In:

SELECT *
FROM A WHERE NOT EXISTS ( SELECT B.Key FROM B WHERE A.Key = B.Key )

Result:

Key     Data        
2       somedata2
like image 7
ntziolis Avatar answered Oct 02 '22 16:10

ntziolis