If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > LEFT Join with multiple tables in SQL query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-04, 10:37
teedee teedee is offline
Registered User
 
Join Date: Sep 2004
Posts: 1
LEFT Join with multiple tables in SQL query

I need an SQL query to retrive columns from 3 related tables a,b,c.
The relations between these tables are in 3 other tables x, y,z.
Out of these table c may or may not have records so we need to relate it with an outer join. c is related to b through table z.
How should I write this query that involves a number of tables and a join.

When 2 tables are involved, it is simple to write a query such as
SELECT P.PARTNUM, P.DESCRIPTION,P.PRICE,
O.NAME, O.PARTNUM
FROM PART P
LEFT OUTER JOIN ORDERS O ON ORDERS.PARTNUM = 54

Now as I have more than 2 tables, how do I write my SQL query?
say I have
select a.field, b.field, c.field from a, b, c, d, e, f where
a.f1=d.f1 and d.f2 = b.f2 and and c.f1=f.f1 and f.f2=b.f1

where do I fit my left outer join clause?
Reply With Quote
  #2 (permalink)  
Old 09-01-04, 13:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
a, b, c

x, y, z

PARTS and ORDERS (bad join condition, by the way)

then a, b, c, d, e, f,

which tables do you want to see in the answer?

okay, i'll make up some too
Code:
select f.foo
     , b.bar
     , q.qux
  from footable as f
left outer
  join bartable as b
    on f.id = b.fid
left outer
  join quxtable as q
    on b.id = q.bid
if you have more than 3 tables, just keep going, follow that pattern
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-01-04, 22:43
sundialsvcs sundialsvcs is offline
Registered User
 
Join Date: Oct 2003
Posts: 706
But beware... when you start introducing OUTER joins, you can quickly cause an explosion of the number of rows that appear in the query! (No matter how many rows are in the final result.)

For example, let's say that tables A, B, and C each have 1,000 records each. The possibility exists that at some point during query-processing the engine is wrestling with 1,000 * 1,000 * 1,000 records. This can quickly exhaust the resources of even the most powerful SQL engine. Whether or not this happens depends upon your exact query, but "the possibility exists" and it needs to be avoided.
__________________
ChimneySweep(R): fast, automatic
table repair at a click of the
mouse! http://www.sundialservices.com
Reply With Quote
  #4 (permalink)  
Old 09-02-04, 01:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
same can be said for INNER joins

it all depends on how the columns used in the joins are indexed
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On