Sub topic 3
Computing Values and Naming Them with Aliases
This chapter assumes you know that a table contains rows and columns, and that SELECT reads data from a table. An expression is a calculation or transformation that SQL evaluates for each selected row.
Calculate values in the select list
A SELECT list can contain more than column names. You can calculate a new value from existing columns.
SELECT first_name, salary * 12 AS annual_salary
FROM employees;For every employee, the database multiplies salary by 12. The result appears as a new output column named annual_salary.
AS creates a column alias. An alias is a temporary name for a column or expression in the query result. It does not rename the column in the table.
You can use arithmetic operators in expressions:
+adds values.-subtracts values.*multiplies values./divides values.
You can also combine columns in an expression.
SELECT first_name,
salary,
salary * 0.10 AS estimated_bonus,
salary + salary * 0.10 AS estimated_total
FROM employees;The database calculates each expression separately for each row. The original salary column remains unchanged.
Choose readable aliases
Aliases make result sets easier to understand, especially when an expression would otherwise receive an unclear database-generated name.
SELECT first_name AS employee_first_name,
salary * 12 AS annual_salary
FROM employees;In many databases, an alias can be written without AS, but using AS makes the query clearer. If an alias contains spaces or reserved words, use the quoting style required by your database. In PostgreSQL and standard SQL, double quotes are commonly used.
SELECT salary * 12 AS "Annual Salary"
FROM employees;Avoid aliases that hide the meaning of a value. annual_salary is clearer than value1.
Where aliases can be used
SQL has a logical processing order. The database first identifies rows from FROM, then applies WHERE, then computes the expressions in SELECT. Because WHERE is processed before the select list, it normally cannot use an alias created in that select list.
ORDER BY is processed later, so it can usually use the alias.
SELECT first_name,
salary * 12 AS annual_salary
FROM employees
WHERE salary > 40000
ORDER BY annual_salary DESC;This query filters using the stored column salary, calculates annual_salary, and sorts by that calculated result.
If you need the expression in WHERE, repeat the expression or use a subquery. Repeating it is simple for a short expression.
SELECT first_name, salary * 12 AS annual_salary
FROM employees
WHERE salary * 12 > 600000;The repeated expression can become inconvenient if the calculation is long. A later chapter can use a subquery to name the result before filtering it.
Common errors
A frequent error is expecting an alias to update the table. It only labels returned data. Another error is using single quotes around an alias:
SELECT salary * 12 AS 'annual_salary'
FROM employees;Some database systems accept this, but single quotes normally mark text values. Prefer the portable form AS annual_salary.
Exercise
Write a query that returns each employee's first name and monthly salary multiplied by 12 under the alias annual_salary. Return only employees whose salary is at least 50000.
Show answer
SELECT firstname, salary * 12 AS annualsalary FROM employees WHERE salary >= 50000;