Skip to content
Advertisement

SQL select from Table1 that has no value in Table2

I have two tables with the following data:

Table 1:

NUMBER1 | NUMBER2 | TYPE
101       201       A
101       202       A
101       203       A
101       204       A
102       211       A
102       212       A
102       213       A

Table 2:

NUMBER1 | NUMBER2 | TYPE
101       201       B
101       202       B
101       203       B
102       211       B
102       212       B

Then I used a join for this Table 3:

NUMBER1 | NUMBER2 | TYPE
101       201       A
101       201       B
101       202       A
101       202       B
101       203       A
101       203       B
101       204       A
102       211       A
102       211       B
102       212       A
102       212       B
102       213       A

I need to get the row with 101 – 204 – A and 102 – 213 – A.

Data is available in Table 1 but has no data aligned in Table 2.

Is there a way to get this?

PS: This is dummy data.

Advertisement

Answer

You can use a left join and select only the records having null in the right table in some column which shouldn’t be null if there was a corresponding record.

SELECT a.NUMBER1, a.NUMBER2, a.TYPE
FROM
    Table_1 a
    LEFT JOIN Table_2 b
        ON a.NUMBER1 = b.NUMBER1 AND
           a.NUMBER2 = b.NUMBER2
WHERE
    b.NUMBER1 IS NULL
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement