Chapter 5. The SELECT Statement
This chapter covers the following exam topics:
Questions on the material in this chapter make up approximately 10% of the exam.
The SELECT statement retrieves information from one or more tables. Retrievals tend to be the most common database operation, so it's important to understand how SELECT works and what you can do with it.
This chapter provides the basic background on how to write SELECT statements and how to use the various parts of its syntax to get the results you want. The examples in this chapter use SELECT statements for retrievals involving no more than a single table. Joins (retrievals that select information from multiple tables in the same query) are covered in a later chapter.
A representative syntax for the SELECT statement is as follows:
SELECT values_to_display
FROM table_name
WHERE expression
GROUP BY how_to_group
HAVING expression
ORDER BY how_to_sort
LIMIT row_count;
Note that the syntax shown here has been simplified; full SELECT syntax includes additional clauses that aren't covered in this chapter.
All clauses following the output column list are optional. For example, you don't need to include a LIMIT clause when writing a SELECT statement. However, any clauses that you do include must be specified in the order shown.
This chapter discusses several clauses of the SELECT statement that allow control over many aspects of query processing:
What values to display and the table from which to retrieve them How to identify the characteristics that define which records to retrieve How to sort output rows How to limit the output to a specific number of the rows retrieved How to arrange rows into groups and calculate aggregate values such as sums or averages for each group How to remove duplicates from query output How to combine results from multiple queries into a single result set
In most cases, the sample queries shown assume that you've already selected a default database. If that isn't true, you can select a database named db_name by issuing a USE db_name statement.
|