Sql – Finding missing number between tables in SQL

oracleoracle10gsql

I'm working on the following 2 tables on Oracle 10g. I'm attempting a rather simple task, but can't get my query right, perhaps due to my lack of understanding of the basics. I want to query out the account_no from TEMP which is not present in BMF.

Two Tables:
Table 1: BMF: 1372 rows

account_no  |  trans_amount   | tracking_id

8149817     |   8100          |     72422912

8197743     |   9100          |     72422913

7165129     |   8100          |     72422914

8625861     |   8100          |     72422915

8463378     |   2100          |     72422916

8213330     |   3100          |     72422917

Table 2: temp : 1373 rows — There is only ONE account_no in TEMP that's missing from BMF

account_no

8149817

8197743

7165129

8625861

8463378

8213330

84633

48

Expected result:
8463348 — As this number is not present in the BMF table.

My Query:

 select a1.account_no from TEMP a1, bmf a2 
 where a2.tracking_id between 72422912 and 72424283
 and a1.account_no != a2.account_no

Any pointers, with a correct query will be helpful

Regards,
novice

Best Answer

SELECT account_no FROM temp 
WHERE NOT EXISTS (SELECT account_no FROM bmf 
                  WHERE bmf.account_no = temp.account_no)


This will have same execution plan as SQL in other answers here, but it states intention more clearly (at least to me).