-1

I have 4 tables I need to join in a query. I want all results from table 1, and then any want to join any results from table 1,2,3. I'm not sure how to write this query.

Here is my best guess:

SELECT
 *
FROM
 t1
INNER JOIN
(
 t2 on t2.id = t1.id
 FULL OUTER JOIN t3 on t3.id = t1.id
 FULL OUTER JOIN t4 on t4.id = t1.id
)

I did find this post that explains the different types of joins. From this I believe I want either a left or right join for table1, but then I am confused about the nested statement.

Community
  • 1
  • 1
spyderman4g63
  • 4,087
  • 4
  • 22
  • 31
  • How are all these tables related? Knowing that will help make the query more efficient. – Joe W Aug 31 '12 at 17:39

1 Answers1

2

The key to your question is that you want all results from table 1. This implies a left outer join:

    select *
    from t1 left outer join
         t2
         on t1.id = t2.id left outer join
         t3
         on t1.id = t3.id left outer join
         t4
         on t1.id = t4.id

The left outer joins keep all the records on the "first" or "before" table, but not records from the second table. Because you start with t1, all those records will be kept with only the matches to t2, and so on.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786