ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Reads →  SELECT clauses →  SELECT - FROM → 

SELECT - JOIN

Quick Reference

Syntax

... [(] { data_source [AS tabalias]}|join
          {[INNER] JOIN}|{LEFT|RIGHT [OUTER] JOIN}|{CROSS JOIN}
             { data_source [AS tabalias]}|join [ON join_cond] [)] ...  .


Addition:

... ON join_cond

Effect

Joins the columns of two or more data sources in a results set of a query in a join expression. A join expression joins a left side with a right side, using

Every join expression for an inner or outer join must contain a join condition join_cond after ON (see below). A join expression for a cross join cannot contain any join conditions.

The following applies to entries specified on the left side and on the right side:

The priority in which nested join expressions are evaluated is specified as follows:

Results set for inner joins

In a single results set, an inner join joins 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, a row is not created in the resulting results set.

Results set for outer joins

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

Results set for cross join

The cross join forms a cross product of the results set of the left side and the results set of the right side. The cross join joins 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 possible combinations of rows. The number of rows in the results set of the cross join is the product of the number of rows of both joined results sets.

Notes

Executable Example

Inner, outer, and cross joins

Addition

... ON join_cond

Effect

Join condition. A join condition must be specified for an inner or outer join. A join condition does not have to be specified for a cross join.

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

the syntax check is performed in a strict mode from Release 7.50, which handles the statement more strictly than the regular syntax check.

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
       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 )
       INTO CORRESPONDING FIELDS OF TABLE @itab.

cl_demo_output=>display( itab ).

Example

Join the columns carrid, carrname, and connid of the database tables scarr and spfli using a left 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 type-friendly initial value (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
       FROM scarr AS s
       LEFT OUTER JOIN spfli AS p ON s~carrid   =  p~carrid
                                  AND p~cityfrom = @p_cityfr
       INTO CORRESPONDING FIELDS OF TABLE @itab.

DELETE itab WHERE connid <> '0000'.

cl_demo_output=>display( itab ).

Example

Cross join of table T000 of all clients of an AS ABAP with the entries for the message classSABAPDEMOS in the table T100. Without the WHERE condition, the results set would be very large.

DATA BEGIN OF wa.
DATA mandt TYPE t000-mandt.
DATA mtext TYPE t000-mtext.
INCLUDE TYPE t100.
DATA END OF wa.
DATA itab LIKE STANDARD TABLE OF wa WITH EMPTY KEY.

SELECT t000~mandt, t000~mtext, t100~*
       FROM t000 CROSS JOIN t100
       WHERE t100~arbgb = 'SABAPDEMOS'
       ORDER BY t000~mandt, t100~sprsl, t100~msgnr
       INTO TABLE @itab.

cl_demo_output=>display( itab ).

Executable Example

Multiple Joins



Continue
Example Inner, Outer, and Cross Joins
Example Multiple Joins