MySQL Aliases
An alias is a temporary name for a column or a table, valid only for the duration of one query. Aliases make results easier to read and long queries easier to write.
Column aliases
expression AS alias names a result column. Without an alias, the column header is the expression text itself:
SELECT CONCAT(first_name, ' ', last_name), salary * 12
FROM employees WHERE employee_id = 4;
SELECT CONCAT(first_name, ' ', last_name) AS full_name,
salary * 12 AS yearly_salary
FROM employees WHERE employee_id = 4;+------------------------------------+-------------+
| CONCAT(first_name, ' ', last_name) | salary * 12 |
+------------------------------------+-------------+
| Linus Berg | 46800.00 |
+------------------------------------+-------------+
+------------+---------------+
| full_name | yearly_salary |
+------------+---------------+
| Linus Berg | 46800.00 |
+------------+---------------+
AS is optional (salary * 12 yearly_salary works), but writing it makes the intent obvious and avoids a classic bug: forget a comma between two columns and the second one silently becomes an alias for the first.
If an alias contains spaces or is a reserved word, quote it with backticks (or double/single quotes):
SELECT name AS `Product name`, unit_price AS `Price (EUR)`
FROM products
WHERE product_id = 1;+--------------------+-------------+
| Product name | Price (EUR) |
+--------------------+-------------+
| Espresso Beans 1kg | 24.50 |
+--------------------+-------------+
Where can you use a column alias?
SQL clauses are not evaluated in the order they are written. Roughly, the server does FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. An alias is created in SELECT, so:
| Clause | Can use a select-list alias? |
|---|---|
WHERE |
no (it runs before SELECT) |
GROUP BY, HAVING |
yes, as a MySQL extension |
ORDER BY |
yes |
SELECT name, unit_price * units_in_stock AS stock_value
FROM products
ORDER BY stock_value DESC
LIMIT 2;+--------------------+-------------+
| name | stock_value |
+--------------------+-------------+
| Espresso Beans 1kg | 980.00 |
| Earl Grey Tea | 573.75 |
+--------------------+-------------+
Using the alias in WHERE fails:
SELECT name, unit_price * units_in_stock AS stock_value
FROM products
WHERE stock_value > 500;
-- ERROR 1054 (42S22): Unknown column 'stock_value' in 'where clause'(MariaDB words the end of the message as in 'WHERE'.) To filter on a calculated value, repeat the expression in WHERE, or wrap the query in a subquery or CTE.
Table aliases
A table alias gives a table a short nickname inside one query. It is essential once a query uses several tables, where columns have to be qualified with their table:
SELECT o.order_id, c.first_name, c.last_name, o.order_date
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
WHERE o.order_id <= 3;+----------+------------+-----------+------------+
| order_id | first_name | last_name | order_date |
+----------+------------+-----------+------------+
| 1 | Ana | Silva | 2025-01-05 |
| 2 | Ben | Carter | 2025-01-09 |
| 3 | Daniel | Weber | 2025-01-18 |
+----------+------------+-----------+------------+
o and c stand for orders and customers. Both tables have a customer_id column, so writing just customer_id would be ambiguous; c.customer_id is not. (Joins are explained in the next lessons.)
Once a table has an alias, you must use the alias: orders.order_id no longer works in that query. Table aliases are also what makes a self join possible, where the same table appears twice under two names.
Tip: Choose aliases that hint at the table (
cfor customers,odfor order_details). Aliases likea,b,cfor unrelated tables make long queries hard to follow.
Test yourself
Why does WHERE stock_value > 500 fail when stock_value is a select-list alias?
- Aliases can't contain underscores
- WHERE is evaluated before SELECT creates the alias
- WHERE only accepts constants
- It needs backticks
What does SELECT first_name last_name FROM customers return?
- Two columns
- One column of first names, titled last_name
- A syntax error
- One column of last names
After FROM orders AS o, how do you refer to the order_id column?
- orders.order_id
- o.order_id
- AS.order_id
- order_id.o