 |
|
23rd August 2007, 09:50
|
#1
|
|
Contractor Among Contractors
Join Date: Jun 2006
Location: London
Posts: 1,663
|
SQL Help please
I have 2 tables, e.g. customers and orders. Foreign key between is customerID. I want to know which customers have zero orders.
What SQL will achieve this?
Thanks
__________________
"Trouble with the French is they have no word for Entrepreneur"
Quote:
Originally Posted by Xenophon
Ruprect is awarded +5 Xeno Geek Points.
...
*In addition, the following posters are awarded +15 Xeno Geek Points:
Ruprect
|
|
|
|
23rd August 2007, 09:54
|
#2
|
|
Godlike
Join Date: Jul 2005
Posts: 10,101
|
something like
select c.field1, c.field2
from customers c
left join orders o
on c.prikey = o.forkey
where o.somefield is null
off the top of me head like.
__________________
By the time you finish reading this sentence, the national debt will have risen by another £1,500 - which you or your children will have to repay.
Just so you know.
|
|
|
23rd August 2007, 09:56
|
#3
|
|
Contractor Among Contractors
Join Date: Jul 2005
Location: the centre of the world - according to Jack Straw
Posts: 1,599
|
Have you not heard of EXISTS or IN ?
select * from customers where customer_id not in (select customer_id from orders)
BTW: you don't say what dbms you're using Oracle, SQL Server, Access, MySQL, etc...
|
|
|
23rd August 2007, 09:57
|
#4
|
|
Godlike
Join Date: Jul 2005
Posts: 10,101
|
Using a subquery is a poor idea. That's what joins are for. You obviously have never worked on databases that get big.
__________________
By the time you finish reading this sentence, the national debt will have risen by another £1,500 - which you or your children will have to repay.
Just so you know.
|
|
|
23rd August 2007, 10:00
|
#5
|
|
Fingers like lightning
Join Date: Jul 2007
Location: Monaco-on-Thames
Posts: 956
|
Quote:
Originally Posted by DimPrawn
Using a subquery is a poor idea. That's what joins are for. You obviously have never worked on databases that get big.
|
Are you sure? Seen the Execution Plan have we? Thought not.
|
|
|
23rd August 2007, 10:01
|
#6
|
|
Contractor Among Contractors
Join Date: Jun 2006
Location: London
Posts: 1,663
|
Thanks dp, gives me what I need I think.
__________________
"Trouble with the French is they have no word for Entrepreneur"
Quote:
Originally Posted by Xenophon
Ruprect is awarded +5 Xeno Geek Points.
...
*In addition, the following posters are awarded +15 Xeno Geek Points:
Ruprect
|
|
|
|
23rd August 2007, 10:08
|
#7
|
|
Godlike
Join Date: Jul 2005
Posts: 10,101
|
Quote:
Originally Posted by richard-af
Are you sure? Seen the Execution Plan have we? Thought not.
|
Don't need to. 9 times out of 10 a join on two columns with appropriate indexes will generate a better execution plan than using IN, which ends up using a hash/match algorithm.
__________________
By the time you finish reading this sentence, the national debt will have risen by another £1,500 - which you or your children will have to repay.
Just so you know.
|
|
|
23rd August 2007, 10:11
|
#8
|
|
Moderator
Join Date: Jul 2005
Posts: 523
|
Quote:
Originally Posted by DimPrawn
Don't need to. 9 times out of 10 a join on two columns with appropriate indexes will generate a better execution plan than using IN, which ends up using a hash/match algorithm.
|
agree. also avoids unneccessary table scans.
|
|
|
23rd August 2007, 10:14
|
#9
|
|
Fingers like lightning
Join Date: Jul 2007
Location: Monaco-on-Thames
Posts: 956
|
Quote:
Originally Posted by scotspine
agree. also avoids unneccessary table scans.
|
9 times out of 10? Unnecessary table scans??!!!
I weep for you both. Amateurs.
|
|
|
23rd August 2007, 10:15
|
#10
|
|
Godlike
Join Date: Jul 2005
Posts: 10,101
|
SP, if anyone else disagrees with me, can you please ban them?

__________________
By the time you finish reading this sentence, the national debt will have risen by another £1,500 - which you or your children will have to repay.
Just so you know.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 21:55.
|  |
| Advertisers |
|
| Contractor Alliance |
Formed a new Ltd Co?
20% off business insurance
£10 off Bauer & Cottrell contract reviews
Find co-workers & client introductions
Increase your value to clients here
|
|