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

select statement with sub query

I have what look like a very simple select query statement
with one sub query, but for some reason, it takes a long
time to come up (1+ min).

select empid, lname, fname from table1
where empid not in (select empid from table2 where HireDate = '01/1/2004')
order by empid.

How can I fix this query so that it would be better or more optimized?

** when I run the sub query on QA, it returns like 10 records, could that
be the cause?
0
graphix03
Asked:
graphix03
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
How about...

select empid, lname, fname
from table1
inner join table2 on table1.SomeID = table2.SomeID
where table2.HireDate = '01/1/2004'
order by table1.empid
0
 
graphix03Author Commented:
But will it do the part I asked for which is where empid in table1 NOT IN table2?
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
... oops, forgot to switch signs around ...

where table2.HireDate <> '01/1/2004'
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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