MySQL Aliases

Aliases give columns and tables temporary, readable names within a query, making complex SQL easier to write and understand.

Why Use Aliases?

An alias is a temporary name assigned to a column or table for the duration of a single query. Aliases exist purely to improve readability and to make otherwise awkward expressions — like computed columns or self-joins — usable. The alias only exists while the query runs; it is never stored in the database schema.

Column Aliases with AS

The AS keyword renames a column or expression in the result set. It is most useful when a column name is unclear, when you compute a value (like a total or a concatenation), or when you want cleaner headers for a report. The AS keyword is technically optional in MySQL — you can write SELECT price total instead of SELECT price AS total — but including it makes queries far easier to read, and most style guides require it.

Table Aliases

Table aliases shorten long or repeated table names, which becomes essential once you start writing JOINs across multiple tables. Instead of prefixing every column with the full table name, you assign a short alias (commonly one or two letters) right after the table name in the FROM or JOIN clause.

  • Column aliases: SELECT column_name AS alias_name
  • Table aliases: FROM table_name AS alias_name (AS is optional here too)
  • Aliases can be referenced in ORDER BY and GROUP BY, but not in the WHERE clause
  • Aliases containing spaces must be wrapped in quotes or backticks
ClauseCan use alias?
SELECTDefines the alias
ORDER BYYes
GROUP BYYes
WHERENo — use the original expression or a subquery
HAVINGYes, in most MySQL configurations

Column alias for a computed value

SELECT product_name,
       price * 0.9 AS discounted_price
FROM products
ORDER BY discounted_price ASC;

Table aliases in a join

SELECT o.order_id, c.customer_name, o.total_amount
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id;

Alias with a space, using backticks

SELECT first_name, last_name,
       CONCAT(first_name, ' ', last_name) AS `Full Name`
FROM employees;
Note: A column alias cannot be used in the WHERE clause of the same query because WHERE is logically evaluated before the SELECT list is computed. If you need to filter on an alias, wrap the query in a subquery or repeat the underlying expression.
Note: Keep table aliases short but meaningful — 'o' for orders and 'c' for customers reads naturally, whereas single letters like 'x' and 'y' force readers to scroll back to the FROM clause constantly.

Exercise: MySQL Aliases

What is the main purpose of a column alias?