Contractor UK Bulletin Board  PayStream

Go Back   Contractor UK Bulletin Board > Contractor UK Forums > Technical
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 23rd August 2007, 09:50   #1
Ruprect
Contractor Among Contractors
 
Ruprect's Avatar
 
Join Date: Jun 2006
Location: London
Posts: 1,663
Default 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 View Post
Ruprect is awarded +5 Xeno Geek Points.
...
*In addition, the following posters are awarded +15 Xeno Geek Points:
Ruprect
Ruprect is offline   Reply With Quote
Old 23rd August 2007, 09:54   #2
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,101
Default

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.
DimPrawn is offline   Reply With Quote
Old 23rd August 2007, 09:56   #3
lilelvis2000
Contractor Among Contractors
 
lilelvis2000's Avatar
 
Join Date: Jul 2005
Location: the centre of the world - according to Jack Straw
Posts: 1,599
Default

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...
lilelvis2000 is offline   Reply With Quote
Old 23rd August 2007, 09:57   #4
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,101
Default

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.
DimPrawn is offline   Reply With Quote
Old 23rd August 2007, 10:00   #5
richard-af
Fingers like lightning
 
richard-af's Avatar
 
Join Date: Jul 2007
Location: Monaco-on-Thames
Posts: 956
Default

Quote:
Originally Posted by DimPrawn View Post
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.
richard-af is offline   Reply With Quote
Old 23rd August 2007, 10:01   #6
Ruprect
Contractor Among Contractors
 
Ruprect's Avatar
 
Join Date: Jun 2006
Location: London
Posts: 1,663
Default

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 View Post
Ruprect is awarded +5 Xeno Geek Points.
...
*In addition, the following posters are awarded +15 Xeno Geek Points:
Ruprect
Ruprect is offline   Reply With Quote
Old 23rd August 2007, 10:08   #7
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,101
Default

Quote:
Originally Posted by richard-af View Post
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.
DimPrawn is offline   Reply With Quote
Old 23rd August 2007, 10:11   #8
scotspine
Moderator
 
Join Date: Jul 2005
Posts: 523
Default

Quote:
Originally Posted by DimPrawn View Post
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.
scotspine is offline   Reply With Quote
Old 23rd August 2007, 10:14   #9
richard-af
Fingers like lightning
 
richard-af's Avatar
 
Join Date: Jul 2007
Location: Monaco-on-Thames
Posts: 956
Default

Quote:
Originally Posted by scotspine View Post
agree. also avoids unneccessary table scans.
9 times out of 10? Unnecessary table scans??!!!

I weep for you both. Amateurs.
richard-af is offline   Reply With Quote
Old 23rd August 2007, 10:15   #10
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,101
Default

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.
DimPrawn is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT. The time now is 21:55.


Advertisers
PayStream

CUK Navigation

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

Fast Company Formation
Same day online company formation £75 + VAT

Form your Ltd Co Here

Contractor Services


 
Content Relevant URLs by vBSEO 2.4.0 © 2005, Crawlability, Inc.