With these syntactic conventions, Listing 4.5 is equivalent to: SELECT au_fname AS “First name”, au_lname AS “Last name”, city AS “City”, state, zip AS “Postal code” FROM authors; ✔ Tips
Trang 1✔ Tips
■ TheSELECTandFROMclauses always are
required to retrieve columns from tables;
all other clauses are optional
■ Closure guarantees that the result of
every SELECTstatement is a table; see
the Tips in “Tables, Columns, and Rows”
in Chapter 2
■ The result in Figure 4.1 contains
dupli-cate rows because two authors live in
San Francisco To remove duplicates,
see “Eliminating Duplicate Rows with
DISTINCT” later in this chapter
■ The rows in your results might be
ordered differently from the rows in
mine; see “Sorting Rows with ORDER BY”
later in this chapter
■ I use NULLto indicate a null in a table
or result; see “Nulls” in Chapter 3
(Listing 4.4 and Figure 4.4).
■ Chapters 7, 8, and 9 describe how to
retrieve columns from multiple tables
■ All results display raw, unformatted values Monetary amounts lack currency signs, and numbers might have an inappropriate number of decimal places, for example Reporting tools—not data-retrieval tools— format data, although DBMSs have non-standard functions that let you format numbers and datetimes in query results
See Microsoft SQL Server’s datename()
function or MySQL’s date_format() function, for example
■ SELECT *often is risky because the num-ber or order of a table’s columns can change and cause your program to fail Likewise, SELECT *won’t be understood
by people unfamiliar with the table’s columns In contrast to queries that name specific columns in the SELECT clause, SELECT *is a resource hog that drags unneeded data across networks (To see how a table is defined, rather than list its rows, see “Displaying Table Definitions” in Chapter 10.)
■ An operation that selects certain columns
from a table is called a projection.
city state country
- -
-New York NY USA
San Francisco CA USA
Berkeley CA USA
from an “invisible” value such as an empty string or a string of spaces.
Listing 4.4 List each publisher’s city, state, and country See Figure 4.4 for the result.
SELECT city, state, country
FROM publishers;
Listing
Trang 2Creating Column Aliases
In the query results so far, I’ve allowed the
DBMS to use default values for column
headings (A column’s default heading in a
result is the source column’s name in the
table definition.) You can use the ASclause
to create a column alias A column alias is
an alternative name (identifier) that you
specify to control how column headings are
displayed in a result Use column aliases if
column names are cryptic, hard to type, too
long, or too short
A column alias immediately follows a column
name in the SELECTclause of a SELECT
state-ment Enclose the alias in single or double
quotes if it’s a reserved keyword or if it
con-tains spaces, punctuation, or special
charac-ters You can omit the quotes if the alias is a
single non-reserved word that contains only letters, digits, or underscores If you want a particular column to retain its default head-ing, omit its ASclause
To create column aliases:
◆ Type:
SELECT column1 [AS] alias1,
column2 [AS] alias2,
columnN [AS] aliasN
FROM table;
column1, column2, …, columnN are
col-umn names; alias1, alias2, …, aliasN are
their corresponding column aliases; and
table is the name of the table that
con-tains column1, column2, ….
Listing 4.5 shows the syntactic variations
of the ASclause Figure 4.5 shows the result
of Listing 4.5
for AS syntax In your programs, pick one construction and use it consistently See Figure 4.5 for the result.
state,
zip 'Postal code'
FROM authors;
Listing
First name Last name City state Postal code
- -
-Sarah Buchman Bronx NY 10468
Wendy Heydemark Boulder CO 80303
Hallie Hull San Francisco CA 94123
Klee Hull San Francisco CA 94123
Christian Kells New York NY 10014
Kellsey Palo Alto CA 94305
Paddy O'Furniture Sarasota FL 34236
Trang 3In standard SQL and most DBMSs, the
key-word ASis optional, but you should always
include it and surround aliases with double
quotes to make your SQL code more
portable and readable With these syntactic
conventions, Listing 4.5 is equivalent to:
SELECT au_fname AS “First name”,
au_lname AS “Last name”,
city AS “City”,
state,
zip AS “Postal code”
FROM authors;
✔ Tips
■ A column alias doesn’t change the name
of a column in a table
■ To determine a column’s name in a
table definition, see “Displaying Table
Definitions” in Chapter 10
■ You can use a reserved keyword if
you quote the alias The query SELECT
SUM(sales) AS “Sum” FROM titles;uses
the reserved word SUMas a column alias,
for example For information about
key-words, see “SQL Syntax” and “Identifiers”
in Chapter 3
■ ASalso is used to name derived columns (whose values are determined by expres-sions other than simple column names); see “Creating Derived Columns” in Chapter 5
■ You also can create table aliases with AS; see “Creating Table Aliases with AS” in Chapter 7
PostgreSQL require the AS keyword for column references
Oracle and DB2 display unquoted
col-umn names and aliases in uppercase
SQL*Plus (Oracle’s command-line
processor) truncates column aliases to the number of characters specified in the table’s column definitions The column alias “Postal code”displays as Postain
aCHAR(5)column, for example
DBMSs have restrictions on embedded spaces, punctuation, and special characters
in aliases; search your DBMS
documen-tation for SELECT or AS.
Trang 4Eliminating Duplicate
Columns often contain duplicate values, and it’s common to want a result that lists each
duplicate only once If I type Listing 4.6 to
list the states where the authors live, the
result, Figure 4.6, contains unneeded
dupli-cates The DISTINCTkeyword eliminates duplicate rows from a result Note that the columns of a DISTINCTresult form a candi-date key (unless they contain nulls)
To eliminate duplicate rows:
◆ Type:
SELECT DISTINCT columns FROM table;
columns is one or more comma-separated
column names, and table is the name
of the table that contains columns
(Listing 4.7 and Figure 4.7).
Listing 4.6 List the states in which the authors live.
See Figure 4.6 for the result.
SELECT state
FROM authors;
Listing
state
-NY
CO
CA
CA
NY
CA
FL
Figure 4.6 Result of Listing 4.6 This result contains
unneeded duplicates of CA and NY.
Listing 4.7 List the distinct states in which the authors
live The keyword DISTINCT eliminates duplicate rows
in the result See Figure 4.7 for the result.
FROM authors;
Listing
state
-NY
CO
CA
FL
Figure 4.7 Result of Listing 4.7 This result has no CA
or NY duplicates.
Trang 5✔ Tips
■ If the SELECT DISTINCTclause contains
more than one column, the values of
all the columns combined determine
the uniqueness of rows The result of
Listing 4.8 is Figure 4.8, which contains
a duplicate row that has two columns
The result of Listing 4.9 is Figure 4.9,
which eliminates the two-column duplicate
■ Although nulls never equal each other
because their values are unknown,
DISTINCTconsiders all nulls to be
dupli-cates of each other SELECT DISTINCT
returns only one null in a result,
regard-less of how many nulls it encounters; see
“Nulls” in Chapter 3
■ TheSELECTstatement syntax includes
the optional ALLkeyword You rarely see
ALLin practice because it denotes the
default behavior: display all rows,
includ-ing duplicates
SELECT columns FROM table;
is equivalent to:
SELECT ALL columns FROM table;
The syntax diagram is:
SELECT [ALL | DISTINCT] columns
FROM table;
■ If a table has a properly defined primary
key, SELECT DISTINCT * FROM table;and
SELECT * FROM table;return identical
results because all rows are unique
■ See also “Aggregating Distinct Values
with DISTINCT” in Chapter 6 and
“Handling Duplicates” in Chapter 15
■ For DISTINCToperations, the DBMS
performs an internal sort to identify
and remove duplicate rows Sorting is
computationally expensive—don’t use
DISTINCTunless you have to do so
Listing 4.8 List the cities and states in which the
authors live See Figure 4.8 for the result.
SELECT city, state FROM authors;
Listing
city state - -Bronx NY Boulder CO New York NY Palo Alto CA San Francisco CA San Francisco CA Sarasota FL
Figure 4.8 Result of Listing 4.8 This result contains a
duplicate row for San Francisco, California.
Listing 4.9 List the distinct cities and states in which
the authors live See Figure 4.9 for the result.
FROM authors;
Listing
city state - -Bronx NY Boulder CO New York NY Palo Alto CA San Francisco CA Sarasota FL
Figure 4.9 Result of Listing 4.9 It’s the city–state
combination that’s considered to be unique, not the value in any single column.
Trang 6Sorting Rows with ORDER BY
Rows in a query result are unordered, so you should view the order in which rows appear
as being arbitrary This situation arises because the relational model posits that row order is irrelevant for table operations You can use the ORDER BYclause to sort rows by a specified column or columns in ascending (lowest to highest) or descending (highest to lowest) order; see the “Sort Order” sidebar in this section The ORDER BYclause always is the last clause in a SELECTstatement
To sort by a column:
◆ Type:
SELECT columns FROM table ORDER BY sort_column [ASC | DESC]; columns is one or more comma-separated
column names, sort_column is the name
of the column on which to sort the result,
and table is the name of the table that contains columns and sort_column.
(sort_column doesn’t have to be in listed
in columns.) Specify ASCfor an ascending sort or DESCfor a descending sort If no sort direction is specified, ASCis assumed
(Listings 4.10 and 4.11, Figures 4.10 and 4.11).
Listing 4.10 List the authors’ first names, last names,
cities, and states, sorted by ascending last name.
ASC keyword is optional (In practice, ASC typically is
omitted.) See Figure 4.10 for the result.
SELECT au_fname, au_lname, city, state
FROM authors
ORDER BY au_lname ASC ;
Listing
au_fname au_lname city state
- -
Kellsey Palo Alto CA
Figure 4.10 Result of Listing 4.10 This result is sorted
in ascending last-name order.
Listing 4.11 List the authors’ first names, last names,
cities, and states, sorted by descending first name The
DESC keyword is required See Figure 4.11 for the result.
SELECT au_fname, au_lname, city, state
FROM authors
ORDER BY au_fname DESC ;
Listing
au_fname au_lname city state
- -
-Wendy Heydemark Boulder CO
Sarah Buchman Bronx NY
Paddy O'Furniture Sarasota FL
Hallie Hull San Francisco CA
Christian Kells New York NY
Kellsey Palo Alto CA
Figure 4.11 Result of Listing 4.11 This result is sorted
in descending first-name order The first name of the
Trang 7To sort by multiple columns:
◆ Type:
SELECT columns
FROM table
ORDER BY sort_column1 [ASC | DESC],
sort_column2 [ASC | DESC],
sort_columnN [ASC | DESC];
columns is one or more
comma-separated column names; sort_column1,
sort_column2, …, sort_columnN are the
names of the columns on which to sort
the result; and table is the name of the
table that contains columns and the sort
columns (The sort columns don’t have to
be in listed in columns.) Rows are sorted
first by sort_column1; then rows that
have equal values in sort_column1 are
sorted by the values in sort_column2, and
so on For each sort column, specify ASC
for an ascending sort or DESCfor a
descending sort If no sort direction is
specified, ASCis assumed (Listing 4.12
and Figure 4.12).
Sort Order
Sorting numeric and datetime values is unambiguous; sorting character strings is complex
A DBMS uses a collating sequence, or collation, to determine the order in which characters
are sorted The collation defines the order of precedence for every character in your character set Your character set depends on the language that you’re using—European languages (a Latin character set), Hebrew (the Hebrew alphabet), or Chinese (ideographs), for example The collation also determines case sensitivity (is ‘A’ < ‘a’?), accent sensitivity (is ‘A’ < ‘À’ ?), width sensitivity (for multibyte or Unicode characters), and other factors such as linguistic practices The SQL standard doesn’t define particular collations and character sets, so each DBMS uses its own sorting strategy and default collation DBMSs provide commands or tools that display the current collation and character set Run the command exec sp_helpsortin Microsoft SQL
Server, for example Search your DBMS documentation for collation or sort order.
Listing 4.12 List the authors’ first names, last names,
cities, and states, sorted by descending city within ascending state See Figure 4.12 for the result.
SELECT au_fname, au_lname, city, state FROM authors
ORDER BY state ASC,
city DESC ;
Listing
au_fname au_lname city state - -
Figure 4.12 Result of Listing 4.12.
Trang 8SQL lets you specify relative column-position numbers instead of column names
inORDER BY The position numbers refer to the columns in the result, not the original table Using column positions saves typing, but the resulting code is unclear and invites mistakes if you reorder the columns in the SELECTclause
To sort by relative column positions:
◆ Type:
SELECT columns FROM table ORDER BY sort_num1 [ASC | DESC],
sort_num2 [ASC | DESC],
sort_numN [ASC | DESC];
columns is one or more
comma-separated column names; and sort_num1,
sort_num2, …, sort_numN are integers
between 1 and the number of columns
in columns, inclusive Each integer
speci-fies the relative position of a column in
columns table is the name of the table
that contains columns (The sort
num-bers can’t refer to a column that’s not
listed in columns.) The sort order is the
same order described in “To sort by multiple columns” earlier in this section
(Listing 4.13 and Figure 4.13).
Listing 4.13 List each author’s first name, last name,
city, and state, sorted first by ascending state (column
4 in the SELECT clause) and then by descending last
name within each state (column 2) See Figure 4.13
for the result.
SELECT au_fname, au_lname, city, state
FROM authors
ORDER BY 4 ASC, 2 DESC ;
Listing
au_fname au_lname city state
- -
-Kellsey Palo Alto CA
Figure 4.13 Result of Listing 4.13.
Sorting by Substrings
To sort results by specific parts of a string,
use the functions described in “Extracting
a Substring with SUBSTRING()” in Chapter
5 For example, this query sorts by the last
four characters of phone:
SELECT au_id, phone
FROM authors
ORDER BY
substr(phone, length(phone)-3);
This query works for Oracle,
DB2, MySQL, and PostgreSQL.
In Microsoft SQL Server, use
substring(phone, len(phone)-3, 4)
In Microsoft Access, use
Mid(phone, len(phone)-3, 4)
Trang 9Sorting and Nulls
Sorting is one of the situations where SQL
departs from the idea that a null isn’t equal
to any other value, including another null
(The logical comparison NULL = NULLis
unknown, not true.) When nulls are sorted,
they all are considered to be equal to one
another The SQL standard leaves it up to
the DBMS to decide whether nulls are either
greater than or less than all non-null values
Microsoft Access, Microsoft SQL Server,
and MySQL treat nulls as the lowest
possi-ble values (Listing 4.14 and Figure 4.14).
Oracle, DB2, and PostgreSQL treat nulls
as the highest possible values See also
“Nulls” in Chapter 3
In Oracle, use NULLS FIRSTorNULLS LAST
with ORDER BYto control null-sorting
behavior For other DBMSs, create a derived
column (see Chapter 5) that flags nulls—
CASE WHEN column IS NULL THEN 0 ELSE 1
END AS is_null, for example—and add it as
the first column (with ASCorDESC) in the
ORDER BYclause
Sorting Speed
The three factors that most affect sorting speed are, in order of importance:
◆ The number of rows selected
◆ The number of columns in the ORDER BYclause
◆ The length of columns in the ORDER BYclause
Always restrict a sort to the minimum number of rows needed Running times of sorting
routines don’t scale linearly with the number of rows sorted—so sorting 10n rows takes much more than 10 times longer than sorting n rows Also try to reduce the number of sorted
columns and the columns’ data-type lengths in the table definition, if possible
Listing 4.14 Nulls in a sort column are listed first or
last, depending on the DBMS See Figure 4.14 for the result.
SELECT pub_id, state, country FROM publishers
ORDER BY state ASC ;
Listing
pub_id state country - -
P04 CA USA P02 CA USA P01 NY USA
Figure 4.14 Result of Listing 4.14 This result is sorted
by ascending state The DBMS in which I ran this query treats nulls as the lowest possible values, so the row with the null state is listed first A DBMS that treats nulls as the highest possible values would list the same row last.
Trang 10✔ Tips
■ You can sort by columns that aren’t listed
in the SELECTclause (Listing 4.15 and Figure 4.15) This technique won’t work
for relative column positions
■ You can specify column aliases instead of column names in ORDER BY(Listing 4.16 and Figure 4.16) See “Creating Column
Aliases with AS” earlier in this chapter
■ You can specify the same column multi-ple times in ORDER BY(but that’s silly)
■ If the ORDER BYcolumns don’t identify each row uniquely in the result, rows with duplicate values will be listed in arbitrary order Although that’s the case
in some of my examples (refer to Fig-ures 4.10, 4.12, and 4.13), you should include enough ORDER BYcolumns to identify rows uniquely, particularly if the result is to be displayed to an end user
■ According to the SQL standard, the ORDER BYclause is part of a CURSOR decla-ration and not the SELECTstatement
Cursors, which are objects defined inside
database applications, are beyond the scope of this book All SQL implementa-tions let you to use ORDER BYin a SELECT statement (because the DBMS builds a cursor invisibly) Standard SQL also lets ORDER BYappear in window functions (not
covered in this book)
■ To sort based on conditional logic, add a CASEexpression to the ORDER BYclause (see “Evaluating Conditional Values with CASE” in Chapter 5) For example, this query sorts by priceiftypeis “history”;
otherwise, it sorts by sales: SELECT title_id, type, price, sales FROM titles
ORDER BY CASE WHEN type = 'history' THEN price ELSE sales END;
to retrieve See Figure 4.15 for the result.
FROM authors
Listing
city state
-
-New York NY
Bronx NY
Sarasota FL
Boulder CO
San Francisco CA
San Francisco CA
Palo Alto CA
Figure 4.15 Result of Listing 4.15 This result is sorted
by ascending zip code Rows might appear to be in
random order if you sort by an undisplayed column,
confusing your end user.
Listing 4.16 This query uses column aliases in the
state
FROM authors
ORDER BY state ASC,
"Last Name" ASC,
"First Name" ASC;
Listing
First Name Last Name state
-
-Hallie Hull CA
Klee Hull CA
Kellsey CA
Wendy Heydemark CO
Paddy O'Furniture FL
Sarah Buchman NY
Christian Kells NY