Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses →  SELECT →  SELECT - FROM →  SELECT - JOIN 

Multiple Joins

This example demonstrates chaining and compounding of multiple joins in the SELECT statement.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA itab TYPE TABLE OF wa WITH EMPTY KEY.
    out->begin_section( `Joins` ).
    out->begin_section( `1. demo1 INNER JOIN demo2` ).
    SELECT t1~a AS a1, t1~b AS b1, t1~c AS c1, t1~d AS d1,
           t2~d AS d2, t2~e AS e2, t2~f AS f2, t2~g AS g2, t2~h AS h2
           FROM
             demo_join1 AS t1
               INNER JOIN
                 demo_join2 AS t2 ON t2~d = t1~d
           INTO CORRESPONDING FIELDS OF TABLE @itab.
    out->write( itab
      )->next_section( `2. demo3 INNER JOIN demo4 ` ).
    SELECT t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
           t4~l AS l4, t4~m AS m4, t4~n AS n4
           FROM
             demo_join3 AS t3
               INNER JOIN
                 demo_join4 AS t4 ON t4~l = t3~l
           INTO CORRESPONDING FIELDS OF TABLE @itab.
    out->write( itab
      )->next_section( `3. (demo1 INNER JOIN demo2) ` &&
                       `LEFT OUTER JOIN demo3` ).
    SELECT t1~a AS a1, t1~b AS b1, t1~c AS c1, t1~d AS d1,
           t2~d AS d2, t2~e AS e2, t2~f AS f2, t2~g AS g2, t2~h AS h2,
           t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3
           FROM
             ( demo_join1 AS t1
                 INNER JOIN
                   demo_join2 AS t2 ON t2~d = t1~d )
                     LEFT OUTER JOIN
                       demo_join3 AS t3 ON t3~l = t2~d
           INTO CORRESPONDING FIELDS OF TABLE @itab.
    out->write( itab
      )->next_section( `4. ((demo1 INNER JOIN demo2) ` &&
                       `LEFT OUTER JOIN demo3) INNER JOIN demo4` ).
    SELECT t1~a AS a1, t1~b AS b1, t1~c AS c1, t1~d AS d1,
           t2~d AS d2, t2~e AS e2, t2~f AS f2, t2~g AS g2, t2~h AS h2,
           t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
           t4~l AS l4, t4~m AS m4, t4~n AS n4
           FROM
             ( ( demo_join1 AS t1
                   INNER JOIN
                     demo_join2 AS t2 ON t2~d = t1~d )
                       LEFT OUTER JOIN
                         demo_join3 AS t3 ON t3~l = t2~d )
                           INNER JOIN
                             demo_join4 AS t4 ON t4~l = t3~l
           INTO CORRESPONDING FIELDS OF TABLE @itab.
    out->write( itab
      )->next_section( `5. (demo1 INNER JOIN demo2) ` &&
                       `LEFT OUTER JOIN (demo3 INNER JOIN demo4)` ).
    SELECT t1~a AS a1, t1~b AS b1, t1~c AS c1, t1~d AS d1,
           t2~d AS d2, t2~e AS e2, t2~f AS f2, t2~g AS g2, t2~h AS h2,
           t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
           t4~l AS l4, t4~m AS m4, t4~n AS n4
           FROM
             ( demo_join1 AS t1
                 INNER JOIN
                   demo_join2 AS t2 ON t2~d = t1~d )
               LEFT OUTER JOIN
                 ( demo_join3 AS t3
                     INNER JOIN
                       demo_join4 AS t4 ON t4~l = t3~l ) ON t3~l = t2~d
           INTO CORRESPONDING FIELDS OF TABLE @itab.
    out->write( itab
      )->next_section( `6. demo1 INNER JOIN demo2 ` &&
                       `LEFT OUTER JOIN demo3 INNER JOIN demo4` ).
    SELECT t1~a AS a1, t1~b AS b1, t1~c AS c1, t1~d AS d1,
           t2~d AS d2, t2~e AS e2, t2~f AS f2, t2~g AS g2, t2~h AS h2,
           t3~i AS i3, t3~j AS j3, t3~k AS k3, t3~l AS l3,
           t4~l AS l4, t4~m AS m4, t4~n AS n4
           FROM
             demo_join1 AS t1
               INNER JOIN
                 demo_join2 AS t2 ON t2~d = t1~d
                   LEFT OUTER JOIN
                     demo_join3 AS t3 ON t3~l = t2~d
                       INNER JOIN
                         demo_join4 AS t4 ON t4~l = t3~l
           INTO CORRESPONDING FIELDS OF TABLE @itab.
    out->display( itab ).

Description

Chaining of up to four database tables using inner and outer joins.

  • Inner join between tables DEMO_JOIN1 and DEMO_JOIN2. The result set contains three rows, in which column D of DEMO_JOIN1 has the same content as column D of DEMO_JOIN2.
  • Inner join between tables DEMO_JOIN3 and DEMO_JOIN4. The result set contains three rows, in which column L of DEMO_JOIN3 has the same content as column L of DEMO_JOIN4.
  • Outer join between result set of point 1 and table DEMO_JOIN3. The result set contains three rows. In addition to the row, in which column L ofDEMO_JOIN3 and column D of DEMO_JOIN2 from the result set of point 1 have the same content, there are two rows, in which the columns of DEMO_JOIN3 are filled with zero values, because they do not fulfill the join condition.
  • Inner join between result set of point 3 and table DEMO_JOIN4. The result set contains a row, in which column L of DEMO_JOIN4 has the same content as column L of DEMO_JOIN3 from the result set of point 3.
  • Outer join between the result set of point 1 and result set of point 2. The result set contains three rows. In addition to the row, in which column L ofDEMO_JOIN3 from the result set of point 2 and column D of DEMO_JOIN2 from the result set of point 1 have the same content, there are two rows, in which the columns from the result set of point 2 are filled with zero values, because they do not fulfill the join condition.
  • Linking database tables using joins without explicit compounding. The statement is identical to the compounded statement of point 4.