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

SELECT - JOIN

Short Reference

Syntax

... [(] {dbtab_left [AS tabalias_left]} | join
          {[INNER] JOIN}|{LEFT [OUTER] JOIN}
            {dbtab_right [AS tabalias_right] ON join_cond} [)] ...  .


Effect

Joins the columns of two or more database tables in a results set.

The syntax of join is a recursively nestable join expression. A join expression consists of a left side and a right side, joined using either [INNER] JOIN or LEFT [OUTER] JOIN. A join expression can be an inner join (INNER) or an outer join (LEFT OUTER). Every join expression can be enclosed in parentheses. When a join expression is used, the SELECT statement is not subject to SAP buffering.

On the left side, either a transparent database table, a view dbtab_left, or another join expression join can be specified. On the right side, a single transparent database table or a view dbtab_right must be specified, alongside join conditions join_cond after ON. In this way, it is possible to specify a maximum of 24 join expressions after FROM; these expressions join 25 transparent database tables or views together. Pooled and cluster tables cannot be joined using join expressions.

For each of the specified database tables, or for each view, AS can be used to specify an alternative table name tabalias. A database table or a view can exist more than once within a join expression, and can have various alternative names.

Results set for inner joins

In a single results set, an inner join links the columns of the rows in the results set of the left side with the columns of the rows in the results set of the right side. This results set contains all combinations of rows whose columns meet the condition join_cond. If there are no rows in the results set of the left and right sides that meet join_cond, then a row is not created in the resulting results set.

Example

See Joins.

Results set for outer joins

In principle, the outer join creates the same results set as the inner join, with the difference that, for each selected row on the left side, at least one row is created in the results set, even if no rows on the right side fulfill the condition join_cond. The columns on the right side that do not meet the condition join_cond are filled with null values.

Note

If columns from the right side are specified after the addition ORDER BY, the sort order in the case of null values may be determined by the database system.

Example

See Joins.

Join condition

The syntax of the join conditions join_cond is the same as for the conditions sql_cond after the addition WHERE, but with the following differences:

Notes

Example

Join of the columns carrname, connid, and fldate of the database tables scarr, spfli, and sflight using two inner joins. This creates a list of flights from p_cityfr to p_cityto. An alias name is assigned to each table.

PARAMETERS: p_cityfr TYPE spfli-cityfrom,
            p_cityto TYPE spfli-cityto.

TYPES: BEGIN OF wa,
         fldate TYPE sflight-fldate,
         carrname TYPE scarr-carrname,
         connid   TYPE spfli-connid,
       END OF wa.

DATA itab TYPE SORTED TABLE OF wa
          WITH UNIQUE KEY fldate carrname connid.

SELECT c~carrname p~connid f~fldate
       INTO CORRESPONDING FIELDS OF TABLE itab
       FROM ( ( scarr AS c
         INNER JOIN spfli AS p ON p~carrid   = c~carrid
                              AND p~cityfrom = p_cityfr
                              AND p~cityto   = p_cityto )
         INNER JOIN sflight AS f ON f~carrid = p~carrid
                                AND f~connid = p~connid ).

cl_demo_output=>display( itab ).

Example

Join of the columns carrid, carrname, and connid of the database tables scarr and spfli using an outer join. For all flights that do not depart from p_cityfr, the column connid is set to the null value that was transformed to the initial value for its type (when passed to the associated data object). All the airlines that do not fly from p_cityfr are displayed.

PARAMETERS p_cityfr TYPE spfli-cityfrom.

TYPES: BEGIN OF wa,
         carrid   TYPE scarr-carrid,
         carrname TYPE scarr-carrname,
         connid   TYPE spfli-connid,
      END OF wa.
DATA  itab TYPE SORTED TABLE OF wa
                WITH NON-UNIQUE KEY carrid.

SELECT s~carrid s~carrname p~connid
       INTO CORRESPONDING FIELDS OF TABLE itab
       FROM scarr AS s
       LEFT OUTER JOIN spfli AS p ON s~carrid   =  p~carrid
                                  AND p~cityfrom = p_cityfr.

DELETE itab WHERE connid <> '0000'.

cl_demo_output=>display( itab ).