Database Reference
In-Depth Information
▼
Analysis
The
SELECT
statement itself is the same as the one used in the previous code
snippet, except that here the calculated field is followed by the text
AS vend_
title
. This instructs SQL to create a calculated field named
vend_title
containing the results of the specified calculation. As you can see in the output,
the results are the same as before, but the column is now named
vend_title
and any client application can refer to this column by name, just as it would
to any actual table column. Indeed, the
ORDER BY
itself uses the calculated
vend_title
.
Tip
Other Uses for Aliases Aliases have other uses, too. Some common uses include
renaming a column if the real table column name contains illegal characters (for exam-
ple, spaces) and expanding column names if the original names are either ambiguous
or easily misread.
Note
Derived Columns Aliases are also sometimes referred to as
derived columns
, so
regardless of the term you run across, they mean the same thing.
Another frequent use for calculated fields is performing mathematical calcula-
tions on retrieved data. Let's take a look at an example. The
orders
table
contains all orders received, and the
orderitems
table contains the individual
items within each order. The following SQL statement retrieves all the items
in order number
20005
:
▼
Input
SELECT prod_id, quantity, item_price
FROM orderitems
WHERE order_num = 20005;
▼
Output
+---------+----------+------------+
| prod_id | quantity | item_price |
+---------+----------+------------+
| ANV01 | 10 | 5.99 |
| ANV02 | 3 | 9.99 |
| TNT2 | 5 | 10.00 |
| FB | 1 | 10.00 |
+---------+----------+------------+