The SELECT statement allows you to form intricate queries that can return exactly the type of data you want to retrieve. It is one of the most common statements you will be using as an SQL programmer, and it is also one of the most flexible and extensive statements in the SQL standard.
The SELECT statement is a query expression that begins with the SELECT keyword and includes a number of elements that form the expression.
The basic syntax for the SELECT statement can be split into several specific clauses that each help to refine the query so that only the required data is returned. The syntax for the SELECT statement can be shown as follows:
SELECT [ DISTINCT | ALL ] { * | <select list> }
FROM <table reference> [ { , <table reference> } . . . ]
[ WHERE <search condition> ]
[ GROUP BY <grouping specification> ]
[ HAVING <search condition> ]
[ ORDER BY <order condition> ]
As you can see, the only required clauses are the SELECT clause and the FROM clause.
All other clauses are optional.
Use of the
SELECT *
form of the SQL SELECT clause is acceptable in situations where the specific columns involved, and their left to right ordering,
are both irrelevant, for example, in an EXISTS invocation. It can be dangerous in other situations, however, because the meaning of that wildcard * can change if (for example) new columns are added to an existing table.
Recommendation: Be on the lookout for such situations and try to avoid them. In particular, do not use
SELECT *
at the outermost level in a
cursor definition. Instead, always name the pertinent columns explicitly.
A similar remark applies to view definitions also.