How to select a row from one table where a row of values ​​does not exist in another table?

Let's say I have two identical tables, A and B, with the string "x".

I want to select all the elements in A, where the value x in does not have any x value from B.

How to do it?

+4
source share
5 answers

You can also do something like this:

SELECT * FROM TableA LEFT JOIN TableB on TableA.X = TableB.X WHERE TableB.X IS NULL 

(For a very simple example in your question, the NOT EXISTS / NOT IN approach is probably preferable, but your real query is more complicated, this is an option that you might want to consider, for instace you need the som information from TableB, where there is a match, but also want to know where she is not)

+3
source

I am having trouble understanding what you need.
In any case, try the following:

 SELECT * FROM tableA WHERE x not IN (SELECT x FROM tableB) 
+2
source
 select * from TableA except select * from TableB 
+2
source

Fastest - Left Join

 SELECT * FROM A LEFT JOIN B ON AX = BX WHERE BX IS NULL 
+1
source

use it:

 select * from a where x not in (select x from b) 
+1
source

Source: https://habr.com/ru/post/1414104/


All Articles