The SELECT statement and querying

The SELECT statement retrieves information from a database for use by the client application. SELECT statements are also called queries. The information is delivered to the client application in the form of a result set. The client can then process the result set. For example, Interactive SQL displays the result set in the Results pane. Result sets consist of a set of rows, just like tables in the database.

SELECT statements contain clauses that define the scope of the results to return. In the following SELECT syntax, each new line is a separate clause. Only the more common clauses are listed here.

SELECT select-list
[ FROM table-expression ]
[ WHERE search-condition ]
[ GROUP BY column-name ]
[ HAVING search-condition ]
[ ORDER BY { expression | integer } ]

The clauses in the SELECT statement are as follows:

  • The SELECT clause specifies the columns you want to retrieve. It is the only required clause in the SELECT statement.

  • The FROM clause specifies the tables from which columns are pulled. It is required in all queries that retrieve data from tables. SELECT statements without FROM clauses have a different meaning, and this section does not discuss them.

    Although most queries operate on tables, queries may also retrieve data from other objects that have columns and rows, including views, other queries (derived tables) and stored procedure result sets.

  • The WHERE clause specifies the rows in the tables you want to see.

  • The GROUP BY clause allows you to aggregate data.

  • The HAVING clause specifies rows on which aggregate data is to be collected.

  • The ORDER BY clause sorts the rows in the result set. (By default, rows are returned from relational databases in an order that has no meaning.)

Most of the clauses are optional, but if they are included then they must appear in the correct order.

 See also