SELECT Statement

Retrieves information from the database.

Quick Links:

Go to Parameters

Go to Examples

Go to Usage

Go to Standards

Go to Permissions

Syntax

SELECTALL | DISTINCT ] [ row-limitation-option1 ] 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 - (back to Syntax)column-name
   | expression [ [ AS ] alias-name ]
   | * }

row-limitation-option1 - (back to Syntax) 
   FIRST 
   | TOP {ALL | limit-expression} [START AT startat-expression ]
  
limit-expression - (back to row-limitation-option1) or (back to row-limitation-option2)
    simple-expression
  
startat-expression - (back to row-limitation-option1) 
    simple-expression

row-limitation-option2 - (back to Syntax)
   LIMIT { [ offset-expression, ] limit-expression 
   | limit-expression OFFSET offset-expression }

offset-expression - (back to row-limitation-option2)
   simple-expression

simple-expression - (back to startat-expression) or (back to offset-expression) or (back to limit-expression)
   integer
   | variable
   | ( simple-expression )
   | ( simple-expression { + | - | * } simple-expression )

Parameters

(back to top)

Examples

(back to top)

Usage

(back to top)

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

(back to top)

  • SQL—ISO/ANSI SQL compliant.
  • SAP Sybase Database product—Supported by SAP Sybase IQ, with some differences in syntax.

Permissions

(back to top)

Requires SELECT privilege on the named tables and views.

Related concepts
Row Limitation Clauses in SELECT Query Blocks
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