1

Say I have three tables in SQL server 2008 R2

SELECT a.*, b.*, c.*
FROM
  Table_A a
  RIGHT JOIN Table_B b ON a.id = b.id
  LEFT JOIN Table_C c ON b.id = c.id

or

SELECT a.*, b.*, c.*
FROM
  Table_A a
  RIGHT JOIN Table_B b ON a.id = b.id
  JOIN Table_C c ON b.id = c.id

also, does it matter if I use b.id or a.id on joining c?

i.e. instead of JOIN Table_C c ON b.id = c.id, use JOIN Table_C c ON a.id = c.id

Thank you!

7
  • 2
    No, you don't have to do a LEFT JOIN after a RIGHT JOIN; you could do an INNER JOIN or another RIGHT JOIN after the first RIGHT JOIN. Yes, it does matter what you join to what, in general. Commented Jan 8, 2013 at 5:24
  • No, you are wrong. You need LEFT JOIN after a RIGHT JOIN. See my answer. Commented Jan 8, 2013 at 6:15
  • Syntactically, and according to the SQL standard, there is no restriction that you must do a LEFT JOIN after a RIGHT JOIN. To formulate a particular query with a specific set of tables, you may need to do a LEFT JOIN after a RIGHT JOIN, but that is a property of a specific query, not of the syntax of SQL. You would be able to reorder the terms in the query. You do have to be careful about which columns you join to which others. Commented Jan 8, 2013 at 6:20
  • Its funny that you answered like when your kid asks you does he have to open an umbrella when it is raining and you say no, there is no restriction that one must open an umbrella when it is raining. So whats the point to use an umbrella in the first place? Sorry, yes, you are right, but I can get nothing from your answer. Commented Jan 8, 2013 at 7:03
  • 1
    The part about you wanting to keep all rows from Table_B is missing in your question. It makes more sense if that is added because then the answer is "Yes you need to outer join Table_C" otherwise the answer is "It depends on what you want the result to be". Commented Jan 8, 2013 at 7:13

3 Answers 3

2

If it doesn't change the semantics of the query, the database server can reorder the joins to run in whichever way it thinks is more efficient.

Usually, if you want to force a certain order, you can use inline view subqueries, as in

SELECT a.*, x.*
FROM
  Table_A a
  RIGHT JOIN
  (
    SELECT *, b.id as id2 FROM Table_B b 
    LEFT JOIN Table_C c ON b.id = c.id
  ) x
  ON a.id = x.id2
1
  • Thanks. No, I do not want any order, just that I worry the inner join after the right join will cancel out the right join effect. Commented Jan 8, 2013 at 5:31
1

According to the definitions:

  • JOIN : Return rows when there is at least one match in both tables
  • LEFT JOIN Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN Return all rows from the right table, even if there are no matches in the left table

The first option would include all raws from the 1st Join on Tables a and b even if there are no matching ones in table c, while the second statement would show only raws which match ones in table c.

regarding the second question i guess it would make a difference, since the 1st join includes all ids from table b, even though there are no matching ones in table a, so once you change your Join creterium to a.id you will get a different set of ids than b.id.

1

Yes, you do need a LEFT JOIN after a RIGHT JOIN

See

http://sqlfiddle.com/#!3/2c079/5/0

http://sqlfiddle.com/#!3/2c079/6/0

If you don't, the (inner) JOIN at the end will cancel out the effect of your RIGHT JOIN.

That wouldn't make any sense to have a RIGHT JOIN if you don't care. And if you care, you will have to add a LEFT JOIN after it.

8
  • It depends on what result you want. The inner join version does not "cancel" the right join as you said. If the right join was canceled by the inner join you use you would get two rows instead of three SQL Fiddle Commented Jan 8, 2013 at 6:29
  • It doesn't cancel the RIGHT JOIN, it merely produces different results. You are joining c to b, not to a. If you didn't have a in your example and was comparing b left join c to b inner join c, you would get the same difference in the results. Perhaps, you meant to use a different example.
    – Andriy M
    Commented Jan 8, 2013 at 6:33
  • The only reason to use RIGHT JOIN is because I want to 'Return all rows from the right table, even if there are no matches in the left table', which is talking about a to b. Now in order to keep that statement correct, a LEFT JOIN is a must after the RIGHT JOIN, otherwise I get less rows. Commented Jan 8, 2013 at 6:38
  • And to Andriy, no I am not joining c to b. If I am, you are correct and I dont need a LEFT JOIN at all, because b has the most ids than c. I am joining c to the resulting table of (a RIGHT JOIN b) and an INNER JOIN alter my table (a RIGHT JOIN b), which means a LEFT JOIN is a must in order NOT to alter my table (a RIGHT JOIN b). Commented Jan 8, 2013 at 6:42
  • Yes, I was about to correct my mistake when I saw your reply. You are right, the third table is joined to the result of the join, not strictly to the second table. However, the join condition doesn't reference a columns. That means the last join's results do not depend on a, meaning they are not cancelling the effect of the right join.
    – Andriy M
    Commented Jan 8, 2013 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.