Naming columns using the AS clause
Which cities are in districts that begin with the letters Min? Name the hq_city column to make more sense.
SELECT district, hq_city AS City FROM aroma.market WHERE district LIKE 'Min%';
The optional AS clause lets you assign a meaningful name to an expression, which makes referring back to the expression easier. When using the AS clause, there are rules on allowable names. In general, valid names must begin with a letter, be no longer than 128 characters, have no blank spaces, and cannot be a SQL keyword. An SQL keyword would be SELECT, FROM, WHERE, and etc. For more detail on SQL keywords, refer to the SQL Reference Guide .
For example, the following AS clause assigns the alias hq_city to the City column:
hq_city AS City
You can assign the alias without using the keyword AS, but it may not be as obvious when you look at your query:
You will note that regardless of capitalization in the expression, column headers are displayed in all uppercase.
The example query returns the same result set as the previous query in this chapter; however, in this case, column aliases are assigned to create headings for the aggregated results.
|Important: If the value contained in the column referenced by the column alias is the result of a set function, it cannot occur in the WHERE clause; however, it can occur in the HAVING clause. For more information on the HAVING clause, see Part 4 of this series.|