How to get inappropriate entries from two tables

See these two example tables:

Table 1:

id    acc_no    name
------------------------
1     14        aaaa
2     16        bbbb
3     18        ccccc
4     25        wwww
5     27        xxxxxxx
6     28        zzzzzzz

Table 2:

sr no   acc_no  amount
----------------------
1       14      2000
2       16      2344
3       18      3200

I need to get acc_no based entries that do not match in table 1, for example:

CONCLUSION:

id   acc_no   name
---------------------
4    25       wwww
5    27       xxxxxxx
6    28       zzzzzzz

When I tried with the request below, the result was unreliable:

SELECT t1.* 
FROM table1 t1
     LEFT OUTER JOIN table2 t2 ON t1.acc_no = t2.acc_no
WHERE t2.acc_no IS NULL

Give your suggestions. What will be the correct ti SQL query to get above the output?

+5
source share
3 answers

to try:

SELECT * 
FROM table1 t1
WHERE t1.acc_no NOT IN (SELECT acc_no FROM table2)
+8
source

Must be:

select t1.id,t1.acc_no,t1.name from table1 t1
     left outer join table2 t2 on t1.acc_no = t2.acc_no
       where
     t2.id is null
+6
source

Try also:

select t1.* from table1 t1 where 
    not exists (
    select 1 from table2 t2 
    where t1.acc_no=t2.acc_no
    )
+1
source

All Articles