SQL – Insert non-existent value to a table from another table

Case study:

You have table A which has a column named ‘code’ and then you have another table called B which has a foreign key relationship with table A on column ‘code’. We want to select all rows in table A that table B does not have which in this case row bbb and ccc.

example: 

Table A:

CODE
aaa
bbb
ccc

Table B:

CODE
aaa

Solution:

SELECT A.CODE FROM A WHERE CODE NOT IN(SELECT CODE FROM B ) 

Leave a Reply

Your email address will not be published. Required fields are marked *