[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

sql 'not in' vs 'not exists'

Posted on 2014-04-01
3
Medium Priority
?
278 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 29

Accepted Solution

by:
sammySeltzer earned 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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