Sub topic 6
Naming Results with Column Aliases
This chapter assumes you can write basic SELECT ... FROM ... WHERE queries and understand common data types. It also assumes you have used GROUP BY and aggregate functions such as COUNT and SUM.
A column alias is a temporary name for a column or computed expression in a query result. You create one with AS:
SELECT salary * 12 AS annual_salary
FROM employees;The expression salary * 12 is evaluated for each row. The result appears under the heading annual_salary. The alias does not rename the column in the table.
Why aliases matter
Expressions can be difficult to read when their results have no useful name:
SELECT first_name,
last_name,
salary * 12
FROM employees;An alias describes the meaning of the computed value:
SELECT first_name,
last_name,
salary,
salary * 12 AS annual_salary
FROM employees;Aliases also make result sets easier to use in reports, applications, and other queries. Use names that describe the result rather than the calculation.
| Expression | Useful alias |
|---|---|
| `salary * 12` | `annual_salary` |
| `price * quantity` | `line_total` |
| `COUNT(*)` | `employee_count` |
| `MAX(order_date)` | `latest_order_date` |
The AS keyword is usually optional, but using it makes the relationship between the expression and its name clear:
SELECT salary * 12 annual_salary
FROM employees;This often works, but AS annual_salary is easier to scan and less likely to be confused with other syntax.
Alias scope
An alias belongs to the result of one query block. It is not automatically available everywhere in that same query.
SQL logically filters rows before it produces the final SELECT list. Therefore, a WHERE clause normally cannot use an alias defined in that SELECT list:
-- Usually invalid
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
WHERE annual_salary > 100000;The database must apply WHERE before it has produced annual_salary.
ORDER BY is evaluated after the SELECT list, so it can usually reference the alias:
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
ORDER BY annual_salary DESC;This is one reason aliases are especially useful for sorting computed results.
Reusing an alias with a subquery
When a filter must use a computed value, calculate the value in an inner query. The outer query can then treat the alias like a column:
SELECT employee_id,
annual_salary
FROM (
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
) AS e
WHERE annual_salary > 100000;The alias e names the derived table, which is the temporary result produced by the inner query.
This pattern avoids repeating the expression:
-- Less readable
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
WHERE salary * 12 > 100000;Naming rules and portability
Unquoted aliases commonly use letters, numbers, and underscores. Avoid spaces and reserved words:
SELECT salary * 12 AS annual_salary
FROM employees;If you need a name containing spaces, quote it according to your database system:
SELECT salary * 12 AS "Annual Salary"
FROM employees;Quoted identifier rules differ between database systems. Double quotes are standard SQL and work in systems such as PostgreSQL and Oracle. MySQL commonly uses backticks unless a compatibility mode changes its behavior. Simple snake_case aliases are more portable.
Some systems also allow aliases in GROUP BY or HAVING, while others restrict or interpret those references differently. For portable SQL, repeat the expression or place the calculation in a subquery when alias scope matters.
Exercise
Why can the following query usually use annual_salary in ORDER BY but not in WHERE?
Show answer
WHERE is processed before the SELECT list creates the alias, while ORDER BY is processed after the result columns are created.