Optimizing my SQL Query - which is better?

I have two ways of doing this query, and would like the experts advice on which is better in terms of performance.

One where statement that says
 pe.PAT_ID NOT IN
  (SELECT PAT_ID FROM Patient_type WHERE PATIENT_TYPE_C IN  (1,2))

Open in new window


or
pt.PATIENT_TYPE_C not in  (1,2)

Open in new window

--where I have to add the table connections in the FROM clause of the main query
Becky EdwardsEpic Clarity DeveloperAsked:
Who is Participating?
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
I think you don't need the LEFT OUTER JOIN since you're filtering by PATIENT_TYPE_C. An INNER JOIN should be better here.

Can you tell me why someone would use the first option?
Because you're not getting the benefit of the relationship between the two tables (PK and FK that you'll use in the JOIN clause).
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
No doubt at all for the 2nd option.
0
 
ste5anSenior DeveloperCommented:
Sorry, but this question makes no senses, cause both options refer to different table alias names.

Please post a concise and complete example. In your case the entire queries.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Vitor MontalvãoMSSQL Senior EngineerCommented:
My understanding is if she should have a JOIN between Patient and Patient_type or if she should use the NOT IN clause.
0
 
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
Thank you Vitor for clarifying.
   Some coworkers said I should not use the Not In Clause, which enables me to filter out certain patients without pulling in the table PATIENT_TYPE into the main query itself.  

So instead of doing this in the where clause:
PAT_ENC.PAT_ID NOT IN
  (SELECT PAT_ID FROM Patient_type WHERE PATIENT_TYPE_C IN  (1,2))

Open in new window


I would do this:
IN THE FROM CLAUSE
Add left outer join PATIENT_TYPE            pt ON PAT_ENC.PAT_ID=pt.PAT_ID
and this:
IN THE WHERE CLAUSE
pt.PATIENT_TYPE_C not in  (1,2)
0
 
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
Also Viktor:
Can you tell me why someone would use the first option?  I copied it from someone else's query.
0
 
Becky EdwardsEpic Clarity DeveloperAuthor Commented:
Thank you Vitor for your assistance.  I will make changes accordingly.  Have a great day!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.