• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

sql 'not in' vs 'not exists'

I am trying to convert a sql that has a sub query with 'not in' clause to a sql with 'not exist' for performance reasons, but i get different results in both my queries. Please help me understand and get the right query that would give me the same results as the 'not in'  query.

NOT IN sql :
		select 
			id, 
			'Patient',
			'Missing Name',
			null,
			'l_c_claim',
			'p_l_name',
			pat_last_name
		from wt_l_c_bill		
		where p_l_name IS NULL
			and id not in (select b_id from app_b_r_v_item where validation_category = 'Patient')

Open in new window


'NOT EXISTS'  sql
		select 
			id, 
			'Patient',
			'Missing Name',
			null,
			'l_c_claim',
			'p_l_name',
			pat_last_name
		from wt_l_c_bill b		
		where NOT EXISTS (Select 1 from app_b_r_v_item v where validation_category = 'Patient' and b.id = v.b_id)

Open in new window


Thank You
0
patd1
Asked:
patd1
2 Solutions
 
sammySeltzerCommented:
Have you considered adding this line to your NOT EXISTS query:

      
where p_l_name IS NULL

Open in new window


and besides, the two queries are different anyway.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you shall add AND b_id IS NOT NULL into the NOT IN for NOT IN and NOT EXISTS to work the same.
but I also confirm that you have
where p_l_name IS NULL
in the first query, but not in the second.
0
 
patd1Author Commented:
thanks got it.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now