ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Reads → SELECT clauses
SELECT - select_clause
Other versions: 7.31 | 7.40 | 7.54
Syntax
... [DISTINCT] select_list ...
Addition
Effect
SELECT clause of a
query statement. The SELECT defines the structure of the results set
of the SELECT statement. It consists of a
SELECT list, which defines the columns of the results set, and an optional addition DISTINCT, which removes duplicative rows from the results set.
The SELECT list select_list
can indicate all columns of a data source using * or it defines the individual columns of the results set using an
SQL expression. It defines the names of the columns in the results set, using optional or mandatory alias names.
Note
The SELECT clause must either be listed as the first clause after the keyword
SELECT (or after the optional addition SINGLE)
or after the FROM clause. After the FROM clause,
the SELECT clause must be introduced using the addition
FIELDS. A FROM clause of the SELECT clause supports tools such as the code completion in ABAP Editor.
Example
SELECT clause after FIELDS, whose SELECT list specifies individual columns plus aggregate expressions as arguments using SQL expressions.
SELECT FROM sflight
FIELDS carrid,
connid,
MAX( seatsmax - seatsocc ) AS seatsfree_max,
MIN( seatsmax - seatsocc ) AS seatsfree_min
GROUP BY carrid, connid
HAVING carrid = 'LH'
INTO TABLE @DATA(result).
Addition
... DISTINCT
Effect
The addition DISTINCT removes rows that occur more than once in a multirow results set. Here the entire row content is taken into consideration.
The addition DISTINCT must not be used in the following cases:
-
If the addition
SINGLEis used. -
If a column specified in the
SELECTlistselect_listhas the type STRING, RAWSTRING, LCHR, LRAW, or GEOM_EWKB.
If DISTINCT is used, the statement SELECT bypasses table buffering.
Notes
-
For the addition
DISTINCT, it is not important which columns of the results set are key fields of the associated database tables, views, or CDS entities. -
The comparison is based on the full row content, which means that the number of rows in the results
set is determined by the
SELECTlist. If, for example, all key fields of a database table are specified directly ascolumnsin a SELECT list, there can be no rows that occur more than once. The other extreme is the case where aSELECTlist contains a single host variable or a single literal. Here, all rows are deleted except one. -
As a part of the
SELECTclause, the additionDISTINCTis applied before the additions UP TO andOFFSETare evaluated. -
If
DISTINCTis specified, it should be noted that this requires the execution of sort operations in the database system, and the statementSELECTtherefore bypasses table buffering.
Example
Gets all destinations to which Lufthansa flies from Frankfurt.
SELECT DISTINCT cityto
FROM spfli
WHERE carrid = 'LH' AND
cityfrom = 'FRANKFURT'
INTO TABLE @DATA(destinations).
cl_demo_output=>display( destinations ).