Sub topic 7
Where an Alias Can and Cannot Be Used
Assume you know basic SELECT, FROM, and WHERE queries, along with GROUP BY and aggregate functions such as COUNT and SUM. This chapter uses standard SQL rules. Some database systems extend those rules, so test portable queries against your target database.
A column alias is a temporary name for an expression in the query result. It makes computed values easier to read.
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees;Here, salary * 12 is the expression. annual_salary is its alias.
Why alias scope matters
SQL has a written order and a logical processing order. The database does not generally process every clause from top to bottom as written. It usually starts by finding rows, then grouping them, then computing the SELECT list, and finally sorting the result.
An alias is created while the SELECT list is evaluated. Therefore, clauses processed earlier cannot normally use that alias. ORDER BY is processed later, so it can normally use one.
| Clause | Can normally reference a SELECT alias? | Reason |
|---|---|---|
| `WHERE` | No | It is evaluated before `SELECT` |
| `GROUP BY` | Usually no | It is evaluated before `SELECT` |
| `HAVING` | Usually no | It is evaluated before `SELECT` |
| `ORDER BY` | Yes | It is evaluated after `SELECT` |
Using an alias in `ORDER BY`
This query creates annual_salary and sorts by it:
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
ORDER BY annual_salary DESC;The DESC keyword sorts from the highest value to the lowest value. Using the alias avoids repeating the expression in ORDER BY.
You can also sort by the expression itself:
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
ORDER BY salary * 12 DESC;The alias is usually clearer, especially when the expression is long.
Why `WHERE` cannot usually use the alias
This query is not portable standard SQL:
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
WHERE annual_salary > 100000;The database must decide which rows pass WHERE before it computes the SELECT list. At that point, annual_salary does not exist as a named result.
You can repeat the expression:
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
WHERE salary * 12 > 100000;However, repeating a complex expression makes maintenance harder. A subquery gives the expression a name first, then lets an outer query filter it.
SELECT employee_id,
annual_salary
FROM (
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
) AS e
WHERE annual_salary > 100000;The inner query produces a temporary result named e. The outer query can use annual_salary as an ordinary column.
Aliases, grouping, and dialect differences
Portable SQL usually repeats the expression in GROUP BY and HAVING, or uses a subquery.
SELECT salary * 12 AS annual_salary,
COUNT(*) AS employee_count
FROM employees
GROUP BY salary * 12
HAVING COUNT(*) > 1
ORDER BY annual_salary;Some systems allow a GROUP BY alias, and some allow a HAVING alias. Relying on those extensions can make a query fail when moved to another database.
A common table expression can make a multi-stage query easier to read:
WITH employee_pay AS (
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
)
SELECT annual_salary,
COUNT(*) AS employee_count
FROM employee_pay
GROUP BY annual_salary
HAVING COUNT(*) > 1
ORDER BY annual_salary;The name employee_pay identifies the intermediate result. Its columns, including annual_salary, are available to the outer query.
Exercise
Why can the following query usually use annualsalary in ORDER BY but not in WHERE? Rewrite it so the filter works without repeating salary * 12: SELECT employeeid, salary * 12 AS annualsalary FROM employees WHERE annualsalary > 100000 ORDER BY annual_salary DESC;
Show answer
ORDER BY is evaluated after the SELECT list, but WHERE is evaluated before it. Put the SELECT in a subquery: SELECT employeeid, annualsalary FROM (SELECT employeeid, salary * 12 AS annualsalary FROM employees) AS e WHERE annualsalary > 100000 ORDER BY annualsalary DESC;