There are three main approaches to this: not exists , not in not exists and left join/is null .
It remains to join NULL
SELECT l.* FROM t_left l LEFT JOIN t_right r ON r.value = l.value WHERE r.value IS NULL
NOT IN
SELECT l.* FROM t_left l WHERE l.value NOT IN ( SELECT value FROM t_right r )
DOES NOT EXIST
SELECT l.* FROM t_left l WHERE NOT EXISTS ( SELECT NULL FROM t_right r WHERE r.value = l.value )
Which one is better? The answer to this question may be better broken down into large specific DBMS vendors. Generally speaking, the use of select... where... in (select...) should be avoided when the number of entries in the subquery is unknown. Some suppliers may limit the size. Oracle, for example, has a limit of 1000 . Itβs best to try all three and show a plan of implementation.
In particular, the PostgreSQL form, NOT EXISTS execution plan, and LEFT JOIN/IS NULL same. Personally, I prefer NOT EXISTS because it shows intent better. After all, the semantics is that you want to find entries in A that do not exist in B.
Old but still PostgreSQL-specific gold: https://explainextended.com/2009/09/16/not-in-vs-not-exists-vs-left-join-is-null-postgresql/
L. Holanda Apr 04 '19 at 23:47 on 2019-04-04 23:47
source share