When or why would you use a right outer join instead of left?

后端 未结 11 613
小蘑菇
小蘑菇 2020-11-27 05:47

Wikipedia states:

\"In practice, explicit right outer joins are rarely used, since they can always be replaced with left outer joins and provide no additional functi

相关标签:
11条回答
  • 2020-11-27 06:06

    I think it's difficult if you don't have right join in this case. ex with oracle.

    with a as(
         select 1 id, 'a' name from dual union all
         select 2 id, 'b' name from dual union all
         select 3 id, 'c' name from dual union all
         select 4 id, 'd' name from dual union all
         select 5 id, 'e' name from dual union all
         select 6 id, 'f' name from dual 
    ), bx as(
       select 1 id, 'fa' f from dual union all
       select 3 id, 'fb' f from dual union all
       select 6 id, 'f' f from dual union all
       select 6 id, 'fc' f from dual 
    )
    select a.*, b.f, x.f
    from a left join bx b on a.id = b.id
    right join bx x on a.id = x.id
    order by a.id
    
    0 讨论(0)
  • 2020-11-27 06:10

    In some SQL databases, there are optimizer hints that tell the optimizer to join the tables in the order in which they appear in the FROM clause - e.g. /*+ORDERED */ in Oracle. In some simple implementations, this might even be the only execution plan available.

    In such cases order of tables in the FROM clause matters so RIGHT JOIN could be useful.

    0 讨论(0)
  • 2020-11-27 06:13

    The only time I would think of a right outer join is if I were fixing a full join, and it just so happened that I needed the result to contain all records from the table on the right. Even as lazy as I am, though, I would probably get so annoyed that I would rearrange it to use a left join.

    This example from Wikipedia shows what I mean:

    SELECT *  
    FROM   employee 
       FULL OUTER JOIN department 
          ON employee.DepartmentID = department.DepartmentID
    

    If you just replace the word FULL with RIGHT you have a new query, without having to swap the order of the ON clause.

    0 讨论(0)
  • 2020-11-27 06:17
    SELECT * FROM table_a
    INNER JOIN table_b ON ....
    RIGHT JOIN table_c ON ....
    

    How else could you quickly/easily inner join the first 2 tables and join with table_c while ensuring all rows in table_c are always selected?

    0 讨论(0)
  • 2020-11-27 06:17

    SQL statements, in addition to being correct, should be as easy to read and expressively concise as possible (because they represent single atomic actions, and your mind needs to grok them completely to avoid unintended consequences.) Sometimes an expression is more clearly stated with a right outer join.

    But one can always be transformed into the other, and the optimizer will do as well with one as the other.

    For quite a while, at least one of the major rdbms products only supported LEFT OUTER JOIN. (I believe it was MySQL.)

    0 讨论(0)
  • 2020-11-27 06:18

    The only reason I can think of to use RIGHT OUTER JOIN is to try to make your SQL more self-documenting.

    You might possibly want to use left joins for queries that have null rows in the dependent (many) side of one-to-many relationships and right joins on those queries that generate null rows in the independent side.

    This can also occur in generated code or if a shop's coding requirements specify the order of declaration of tables in the FROM clause.

    0 讨论(0)
提交回复
热议问题