Chapter 8. Understanding the SELECT Statement
This chapter covers selecting data from the RDBMS tables using the
SELECT statement. As the name implies, it deals with selecting data from the RDBMS objects — tables or views — either to be presented to the users, or for some internal purpose. This is the only statement of the data query language (DQL) group.
The use of
SELECT is relatively simple, but it rarely executes without optional clauses, and that's where the fun begins. The
SELECT statement clauses are probably the most confusing in SQL and have to be dealt with accordingly. This chapter introduces the topic, covering the use of sub-queries, compound operators, aggregate functions, and more.
If you want to follow the SQL examples in this chapter and in the following chapters, you can download and run a set of SQL scripts that create (or re-create, if necessary) ACME tables in the RDBMS of your choice (Oracle 11 g, DB2 9.5, or Microsoft SQL Server 2008) and populate them with sample data. Follow links on the accompanying SQL Bible website.
Single Table SELECT Statement Syntax
Following is the generic
SELECT statement, as it is defined by the SQL:2003 standard, for selecting data from a single table. The query includes the
SELECT command, followed by the list of identifiers (table or view columns); then comes the mandatory
FROM clause (optional in Microsoft SQL Server 2008) that contains names of the tables, from which these columns are selected.
The rest of the clause is optional, ...