Solved

sql 'not in' vs 'not exists'

Posted on 2014-04-01
3
256 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
3 Comments
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 250 total points
Comment Utility
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 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Comment Utility
thanks got it.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now