Solved

sql 'not in' vs 'not exists'

Posted on 2014-04-01
3
269 Views
Last Modified: 2014-04-01
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
Comment
Question by:patd1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 250 total points
ID: 39969444
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39969531
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
 

Author Closing Comment

by:patd1
ID: 39969571
thanks got it.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question