In many ways, the SELECT statement is the real heart of SQL. It lets you find and view your data in a variety of ways. You use it to answer questions based on your data: how many, where, what kind of, even what if. Once you become comfortable with its sometimes dauntingly complex syntax, you'll be amazed at what the SELECT statement can do.
This chapter begins with the bare bones: the SELECT, FROM, and WHERE clauses, search conditions, and expressions.
Chapter 5 delves into some SELECT refinements: ORDER BY, the DISTINCT keyword, and aggregates.
Chapter 6 covers the GROUP BY clause, the HAVING clause, and making reports from grouped data. Chapter 6 also summarizes the issues regarding null values in database management.
Chapter 7 introduces multiple-table queries with a comprehensive discussion of joining tables.
Chapter 8 moves on to nested queries, also known as subqueries.
The FROM clause specifies the tables those columns are in.
The WHERE clause qualifies the rowsit chooses the ones you want to see.
Collectively, these are expressions. Because the column name expression is the simplest case, examples often start there and then go on to a more complex expression. This does not mean that a column name is not an expressionit's just the place to start looking at expressions. Syntax that includes "expression" or "expr" or "char_expr" means that you can use a column name or a more complex expression.
Artful combinations of the SELECT, FROM, and WHERE clauses produce meaningful answers to your questions and keep you from drowning in a sea of data. Think of the SELECT and WHERE clauses as horizontal and vertical axes on a matrix. (Figure 4.1 illustrates the query you saw at the beginning of the chapter.) The data you get from the SELECT statement is at the intersection of the SELECT (column) and WHERE (row) clauses.
Let's look at a SELECT statement with another bookbiz table, authors. The authors table stores information about authors: ID numbers, names, addresses, and phone numbers. If you want to know just the names of authors who live in California (not their addresses and phone numbers), use the SELECT clause and the WHERE clause to limit the data that the SELECT statement returns.
Here's a query that uses the SELECT clause's select_list to limit the columns you see. It lists just the names for the authors, ignoring their ID numbers, addresses, and phone numbers.
This display still doesn't provide exactly what you want because it lists all authors regardless of the state they live in. You need to refine the data retrieval statement further with the WHERE clause.
Now you're looking at just the names of the 15 authors having a California address. The rows for the eight authors living elsewhere are not included in the display.
Although SQL is a free-form language, you do have to keep the clauses in a SELECT statement in syntactical order (for example, a GROUP BY clause must come before an ORDER BY clause). Otherwise, you'll get syntax errors.
You may need to qualify the names of database objects (according to the customs of your SQL dialect) if there is any ambiguity about which object you mean. In this database, there are several columns called title_id (in the titles table, the titleauthors table, and the titleview view, among otherssee Figure 2.13). When you are working with multiple tables, you may have to specify which title_id column you're talking about by including the table or view name, usually separated from the column name by a period (titles.title_id). If the system allows multiple tables with the same name, add the owner name (mary.titles.title_id or dba.titles.title_id)some possible combinations appear in Figure 4.2. You may also see larger elements, such as database and server names, used this way, but that is less common.
The examples in this chapter involve queries on a single table, so qualification is not an important issue here. Qualifiers are also omitted in most books, articles, and reference manuals on SQL because the short forms make SELECT statements more readable. However, it's never wrong to include them.