473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help With Syntax

I have two tables.
The first table (a) has a list of all my clients.
The second table (b) contains all of the clients who have placed
orders.
How do I get a list of all of the clients from table "a"
who have NOT placed an order based on table "b?"
Keeping in mind, that if they have not placed an order, they will not
appear in table "b."

Both tables have a client_id column

Help

Jun 27 '06 #1
5 1039
Try one of these:

SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
tableB)

SELECT a.*
FROM tableA a
LEFT JOIN tableB b ON a.client_id = b.client_id
WHERE b.client_id IS NULL

you should see better performance on the second one if you have a lot
of rows in your tables.

Jason

Jun 27 '06 #2
be carefull with NULLS and IN, if tableB has even 1 NULL for the
client_id column nothing will be returned
better to add IS NOT NULL

SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
tableB WHERE client_id IS NOT NULL )

or use NOT EXISTS

SELECT * FROM tableA A WHERE NOT EXISTS (SELECT * FROM
tableB WHERE client_id = A.client_id )
Denis the SQL Menace
http://sqlservercode.blogspot.com/
Jason wrote:
Try one of these:

SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
tableB)

SELECT a.*
FROM tableA a
LEFT JOIN tableB b ON a.client_id = b.client_id
WHERE b.client_id IS NULL

you should see better performance on the second one if you have a lot
of rows in your tables.

Jason


Jun 27 '06 #3
SQL Menace (de********@gmail.com) writes:
or use NOT EXISTS

SELECT * FROM tableA A WHERE NOT EXISTS (SELECT * FROM
tableB WHERE client_id = A.client_id )


Yes, this is the preferred syntax for the given problem, as it clearly
expresses what it's all about.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '06 #4
Thanks, Jason. I got it to work using basically this core of code. I
had simplified the question for the purpose of getting a straight
answer, but your code is the core of the soluton. Much appreciated!!
Jason wrote:
Try one of these:

SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
tableB)

SELECT a.*
FROM tableA a
LEFT JOIN tableB b ON a.client_id = b.client_id
WHERE b.client_id IS NULL

you should see better performance on the second one if you have a lot
of rows in your tables.

Jason


Jun 28 '06 #5
>> The first table (a) has a list of all my clients. The second table (b) contains all of the clients who have placed orders. <<

The quick answer is to use a LEFT OUTER JOIN or an EXISTS() predicate.
The right answer is to ask why you consider these clients to be
logically diffferent entities. It sounds like what you need is one
table with a status code of some kind to tell you when you have a
client who placed an order versus one who has not (and who returns
orders, etc.).

Why persist redundant data in physical storage? This is called
"attribute splitting" -- you are converting an attribute into a table.

Jun 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: sam | last post by:
When I run this SQL query: SELECT u.*, o.* FROM users u, orders o WHERE TO_DAYS(o.order_date) BETWEEN TO_DAYS('2003-09-20')-10 AND TO_DAYS('2003-09-20')+10
5
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
6
by: Scott Nixon | last post by:
New to Postgres 7.3 from 7.0. Am having some trouble with a query that worked in 7.0 but not in 7.3.....can't seem to figure out the syntax or find info about how to do this anywhere. ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
6
by: toch3 | last post by:
i am writing a c program that is basically an address book. the only header we are using is #include<stdio.hwe are to use a global array, loops, and pointers. we are to have a menu at the...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.