By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

SQL Statement select from one table where not in another table

P: n/a
I have two tables with a 1-many relationship. I want to write a
select statement that looks in the table w/many records and compares
it to the records in the primary table to see if there are any records
that do not match based on a certain field.

Here is how my tables are setup:

Task Code Table
TCode,TCodeFName,Active

Tracking Table(multiple records)
ID,User,ProjectCode,TCode,Hours,Date

I want to look at the tracking table and see if there are any TCode
listed here that are not listed in the Task Code table.

How do I write a SQL statement to do this?
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Michael,

Try this:

select TCode
from Tracking
where not exists (select *
from Task
where Task.TCode = Tracking.TCode
)

Shervin

"Michael" <lu****@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
I have two tables with a 1-many relationship. I want to write a
select statement that looks in the table w/many records and compares
it to the records in the primary table to see if there are any records
that do not match based on a certain field.

Here is how my tables are setup:

Task Code Table
TCode,TCodeFName,Active

Tracking Table(multiple records)
ID,User,ProjectCode,TCode,Hours,Date

I want to look at the tracking table and see if there are any TCode
listed here that are not listed in the Task Code table.

How do I write a SQL statement to do this?

Jul 20 '05 #2

P: n/a
Bas
This is usually faster by the way, I use it a lot:

SELECT Tracking.TCode FROM TaskCode RIGHT OUTER JOIN
Tracking ON TaskCode.TCode = Tracking.TCode
WHERE TaskCode.TCode IS NULL

Cheers,

Bas

"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message
news:vp************@corp.supernews.com...
Michael,

Try this:

select TCode
from Tracking
where not exists (select *
from Task
where Task.TCode = Tracking.TCode
)

Shervin

"Michael" <lu****@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
I have two tables with a 1-many relationship. I want to write a
select statement that looks in the table w/many records and compares
it to the records in the primary table to see if there are any records
that do not match based on a certain field.

Here is how my tables are setup:

Task Code Table
TCode,TCodeFName,Active

Tracking Table(multiple records)
ID,User,ProjectCode,TCode,Hours,Date

I want to look at the tracking table and see if there are any TCode
listed here that are not listed in the Task Code table.

How do I write a SQL statement to do this?


Jul 20 '05 #3

P: n/a
Bas
By the way, it's best to not use reserved words like ID,User and Date as
column names :)

Bas

"Michael" <lu****@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
I have two tables with a 1-many relationship. I want to write a
select statement that looks in the table w/many records and compares
it to the records in the primary table to see if there are any records
that do not match based on a certain field.

Here is how my tables are setup:

Task Code Table
TCode,TCodeFName,Active

Tracking Table(multiple records)
ID,User,ProjectCode,TCode,Hours,Date

I want to look at the tracking table and see if there are any TCode
listed here that are not listed in the Task Code table.

How do I write a SQL statement to do this?

Jul 20 '05 #4

P: n/a
Bas,

It's interesting! I tested these three queries on two big tables:

select TCode
from Tracking
where not exists (select *
from Task
where Task.TCode = Tracking.TCode
)

select TCode
from Tracking
where TCode not in (select TCode
from Task
)

select Tracking.TCode
from Tracking left outer join Task on Task.TCode = Tracking.TCode
where Task.TCode is null
Then I checked the execution plan for all of them. The first two queries
took 32.87% and the last one took 34.25% of execution time of whole batch.
Both tables have indexes on TCode field. The third query has an extra step
in it which is filtering the result set and eliminating records in which
Task.TCode is null.
As you see it's not a huge difference between results. I guess it completely
depends on how you setup your indexes. But still it seems to me that using
EXISTS should be faster. :-)
Is it possible to check it on your problem and compare the results? I'm
interested to know what makes the third query faster in your case.

Thanks,
Shervin


"Bas" <nomailplease> wrote in message
news:3f***********************@dreader4.news.xs4al l.nl...
This is usually faster by the way, I use it a lot:

SELECT Tracking.TCode FROM TaskCode RIGHT OUTER JOIN
Tracking ON TaskCode.TCode = Tracking.TCode
WHERE TaskCode.TCode IS NULL

Cheers,

Bas

"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message
news:vp************@corp.supernews.com...
Michael,

Try this:

select TCode
from Tracking
where not exists (select *
from Task
where Task.TCode = Tracking.TCode
)

Shervin

"Michael" <lu****@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
I have two tables with a 1-many relationship. I want to write a
select statement that looks in the table w/many records and compares
it to the records in the primary table to see if there are any records
that do not match based on a certain field.

Here is how my tables are setup:

Task Code Table
TCode,TCodeFName,Active

Tracking Table(multiple records)
ID,User,ProjectCode,TCode,Hours,Date

I want to look at the tracking table and see if there are any TCode
listed here that are not listed in the Task Code table.

How do I write a SQL statement to do this?



Jul 20 '05 #5

P: n/a
Bas (nomailplease) writes:
This is usually faster by the way, I use it a lot:

SELECT Tracking.TCode FROM TaskCode RIGHT OUTER JOIN
Tracking ON TaskCode.TCode = Tracking.TCode
WHERE TaskCode.TCode IS NULL


Maybe it was in SQL 6.5. I would not expect so in SQL 2000. These two
queries have very similar query plans:

select * from Northwind..Customers c
where not exists (select * from
Northwind..Orders o where o.CustomerID = c.CustomerID)

select c.*
FROM Northwind..Customers c
LEFT JOIN Northwind..Orders o on c.CustomerID = o.CustomerID
WHERE o.OrderID IS NULL

But the latter has a extra filter step the first query don't need.

I advice against using outer joins for NOT EXISTS queries. NOT EXISTS
more clearly expresses what you are after. Another advantage to use
NOT EXISTS is that does require you to know what is LEFT and RIGHT.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6

P: n/a
Bas
Hmm funny!
Must have been som MS Access knowledge from long ago that got stuck with me.
I checked, Access uses the technique itself in one of its wizards to find
unmatched records.

Thanks for pointing this out.

Bas

"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message
news:vp************@corp.supernews.com...
Bas,

It's interesting! I tested these three queries on two big tables:

select TCode
from Tracking
where not exists (select *
from Task
where Task.TCode = Tracking.TCode
)

select TCode
from Tracking
where TCode not in (select TCode
from Task
)

select Tracking.TCode
from Tracking left outer join Task on Task.TCode = Tracking.TCode
where Task.TCode is null
Then I checked the execution plan for all of them. The first two queries
took 32.87% and the last one took 34.25% of execution time of whole batch.
Both tables have indexes on TCode field. The third query has an extra step
in it which is filtering the result set and eliminating records in which
Task.TCode is null.
As you see it's not a huge difference between results. I guess it completely depends on how you setup your indexes. But still it seems to me that using
EXISTS should be faster. :-)
Is it possible to check it on your problem and compare the results? I'm
interested to know what makes the third query faster in your case.

Thanks,
Shervin


"Bas" <nomailplease> wrote in message
news:3f***********************@dreader4.news.xs4al l.nl...
This is usually faster by the way, I use it a lot:

SELECT Tracking.TCode FROM TaskCode RIGHT OUTER JOIN
Tracking ON TaskCode.TCode = Tracking.TCode
WHERE TaskCode.TCode IS NULL

Cheers,

Bas

"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message
news:vp************@corp.supernews.com...
Michael,

Try this:

select TCode
from Tracking
where not exists (select *
from Task
where Task.TCode = Tracking.TCode
)

Shervin

"Michael" <lu****@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
> I have two tables with a 1-many relationship. I want to write a
> select statement that looks in the table w/many records and compares
> it to the records in the primary table to see if there are any records > that do not match based on a certain field.
>
> Here is how my tables are setup:
>
> Task Code Table
> TCode,TCodeFName,Active
>
> Tracking Table(multiple records)
> ID,User,ProjectCode,TCode,Hours,Date
>
> I want to look at the tracking table and see if there are any TCode
> listed here that are not listed in the Task Code table.
>
> How do I write a SQL statement to do this?



Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.