SELECT Statement

Retrieves information from the database.

Syntax

SELECTALL | DISTINCT ] [ FIRST | TOP number-of-rows ] select-list
   … [ INTOhost-variable-list | variable-list | table-name } ]
   … [ INTO LOCAL TEMPORARY TABLEtable-name } ]
   … [ FROM table-list ]
   … [ WHERE search-condition ]
   … [ GROUP BYexpression [, …]
         | ROLLUPexpression [, …] )
         | CUBEexpression [, …] ) ] ] 
   … [ HAVING search-condition ]
   … [ ORDER BYexpression | integer } [ ASC | DESC ] [, …] ]
   | [ FOR JSON json-mode ] 
   … [ row-limitation-option ]
select-list:
column-name
   | expression [ [ AS ] alias-name ]
   | * }
row-limitation-option:
   LIMIT { [ offset-expression, ] limit-expression 
   | limit-expression OFFSET offset-expression }
limit-expression:
simple-expression
offset-expression:
simple-expression
simple-expression:
integer
   | variable
   | ( simple-expression )
   | ( simple-expression { + | - | * } simple-expression )

Parameters

Examples

Usage

You can use a SELECT statement in Interactive SQL to browse data in the database or to export data from the database to an external file.

You can also use a SELECT statement in procedures or in Embedded SQL. The SELECT statement with an INTO clause is used for retrieving results from the database when the SELECT statement returns only one row. (Tables created with SELECT INTO do not inherit IDENTITY/AUTOINCREMENT tables.) For multiple-row queries, you must use cursors. When you select more than one column and do not use #table, SELECT INTO creates a permanent base table. SELECT INTO #table always creates a temporary table regardless of the number of columns. SELECT INTO table with a single column selects into a host variable.

Note: When writing scripts and stored procedures that SELECT INTO a temporary table, wrap any select list item that is not a base column in a CAST expression. This guarantees that the column data type of the temporary table is the required data type.

Tables with the same name but different owners require aliases. A query without aliases returns incorrect results:

SELECT * FROM user1.t1
WHERE NOT EXISTS
(SELECT *
FROM user2.t1
WHERE user2.t1.col1 = user1.t.col1);

For correct results, use an alias for each table:

SELECT * FROM user1.t1 U1
WHERE NOT EXISTS
(SELECT *
FROM user2.t1 U2
WHERE U2.col1 = U1.col1);

The INTO clause with a variable-list is used only in procedures.

In SELECT statements, a stored procedure call can appear anywhere a base table or view is allowed. Note that CIS functional compensation performance considerations apply. For example, a SELECT statement can also return a result set from a procedure.

Standards

  • SQL—ISO/ANSI SQL compliant.

  • Sybase—Supported by SAP Sybase IQ, with some differences in syntax.

Permissions

Requires SELECT privilege on the named tables and views.

Related reference
CREATE VIEW Statement
DECLARE CURSOR Statement [ESQL] [SP]
FETCH Statement [ESQL] [SP]
FROM Clause
MAX_CUBE_RESULT Option
OPEN Statement [ESQL] [SP]
UNION Operation
RESERVED_KEYWORDS Option
ROW_COUNT Option
SUBQUERY_CACHING_PREFERENCE Option