473,489 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Statement select from one table where not in another table

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
6 130038
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1435
by: margraft | last post by:
Hello, I'm somewhat of a newbie and I need to create a view with a column that is not derived from any other tables. What I want is for this field to be an auto-increment field or some kind of...
10
26037
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
3
2445
by: bitoulis | last post by:
Hi, is it possible to use the records of a table as the field names of another table? If yes, how is it done? Thanks in advance Laertes
7
8375
by: rcamarda | last post by:
I wish to build a table based on values from another table. I need to populate a table between two dates from another table. Using the START_DT and END_DT, create records between those dates. I...
1
5821
by: bpforte | last post by:
Hello, I need help with building query, basically I need to select all records from one table that don't exists in second table with status 1, but they can exists in second table with status 0, to...
5
7596
by: Lennart | last post by:
I really like the construction: select * from new table (update ....) X but I noticed that it cant be used as: insert into T select * from new table (update ....) X because of:
4
6586
by: paulcrowsnest | last post by:
Hi everyone This is probably really simple, but it has me stumped. I have a form which gets its data from a table called "Dentistry", this table is linked to another table called...
0
1853
by: =?Utf-8?B?S3VydCBvZiBTYW4gSm9zZQ==?= | last post by:
All- Please advise as to what's the best way to display data in a column in a grid view (and I'm using a dataset) in a table (say TABLE2) which: - is non-zero only if a boolian field in the...
1
2803
by: dannix | last post by:
Hi, I hope I can explain this right: Here is my statement so far: SELECT shift_log.note, shift_log.id, shift_log.timestamp, shift_log.monitor, shift_log.handover, shift_log.start_time,...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.