SQL for Smarties | SQL Programming Style | Trees and Hierarchies in SQL | SQL Puzzles and Answers | Data and Databases


Monday, July 03, 2006

Help With Syntax

SQL Apprentice Question
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

Celko Answers
>> 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.

No comments: