How do I find records that are not joined?

SqlSelectJoinAnti Join

Sql Problem Overview


I have two tables that are joined together.

A has many B

Normally you would do:

select * from a,b where b.a_id = a.id

To get all of the records from a that has a record in b.

How do I get just the records in a that does not have anything in b?

Sql Solutions


Solution 1 - Sql

select * from a where id not in (select a_id from b)

Or like some other people on this thread says:

select a.* from a
left outer join b on a.id = b.a_id
where b.a_id is null

Solution 2 - Sql

select * from a
left outer join b on a.id = b.a_id
where b.a_id is null

Solution 3 - Sql

Another approach:

select * from a where not exists (select * from b where b.a_id = a.id)

The "exists" approach is useful if there is some other "where" clause you need to attach to the inner query.

Solution 4 - Sql

SELECT id FROM a
EXCEPT
SELECT a_id FROM b;

Solution 5 - Sql

You will probably get a lot better performance (than using 'not in') if you use an outer join:

select * from a left outer join b on a.id = b.a_id where b.a_id is null;

Solution 6 - Sql

The following image will help to understand SQL LET JOIN :

enter image description here

Solution 7 - Sql

SELECT <columnns>
FROM a WHERE id NOT IN (SELECT a_id FROM b)

Solution 8 - Sql

Another way of writing it

select a.*
from a 
left outer join b
on a.id = b.id
where b.id is null

Ouch, beaten by Nathan :)

Solution 9 - Sql

This will protect you from nulls in the IN clause, which can cause unexpected behavior.

select * from a where id not in (select [a id] from b where [a id] is not null)

Solution 10 - Sql

In case of one join it is pretty fast, but when we are removing records from database which has about 50 milions records and 4 and more joins due to foreign keys, it takes a few minutes to do it. Much faster to use WHERE NOT IN condition like this:

select a.* from a
where a.id NOT IN(SELECT DISTINCT a_id FROM b where a_id IS NOT NULL)
//And for more joins
AND a.id NOT IN(SELECT DISTINCT a_id FROM c where a_id IS NOT NULL)

I can also recommended this approach for deleting in case we don't have configured cascade delete. This query takes only a few seconds.

Solution 11 - Sql

The first approach is

select a.* from a where a.id  not in (select b.ida from b)

the second approach is

select a.*
  from a left outer join b on a.id = b.ida
  where b.ida is null

The first approach is very expensive. The second approach is better.

With PostgreSql 9.4, I did the "explain query" function and the first query as a cost of cost=0.00..1982043603.32. Instead the join query as a cost of cost=45946.77..45946.78

For example, I search for all products that are not compatible with no vehicles. I've 100k products and more than 1m compatibilities.

select count(*) from product a left outer join compatible c on a.id=c.idprod where c.idprod is null

The join query spent about 5 seconds, instead the subquery version has never ended after 3 minutes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSixty4BitView Question on Stackoverflow
Solution 1 - SqlalberteinView Answer on Stackoverflow
Solution 2 - SqlJoseph AndersonView Answer on Stackoverflow
Solution 3 - SqlMatt HamiltonView Answer on Stackoverflow
Solution 4 - SqlonedaywhenView Answer on Stackoverflow
Solution 5 - SqlnathanView Answer on Stackoverflow
Solution 6 - SqlMonsif EL AISSOUSSIView Answer on Stackoverflow
Solution 7 - SqlBlackWaspView Answer on Stackoverflow
Solution 8 - SqlshahkalpeshView Answer on Stackoverflow
Solution 9 - SqlAmy BView Answer on Stackoverflow
Solution 10 - SqlPetr ŠtipekView Answer on Stackoverflow
Solution 11 - SqlDaniele LicitraView Answer on Stackoverflow