SQL guide · Querying

SQL AS: Rename Columns and Tables With Aliases

The AS keyword creates an alias, a temporary name for a column or a table that exists only for the duration of the query. Aliases do not change anything stored in the database. They make output readable, shorten long table names, and give a name to calculated expressions that would otherwise have none.

A column alias goes in the select list and becomes the heading of that column in the result set. This is especially useful for expressions such as price * quantity or aggregates such as SUM(amount), where the database would otherwise label the column with the raw expression or a generated name. Tools and applications that read your result usually rely on these names, so a clear alias is part of a good query contract.

A table alias goes in the FROM or JOIN clause and lets you refer to the table by a short name. Aliases are required when you join a table to itself, as in a manager and employee hierarchy, because the database needs two different names to tell the two copies apart. In most databases the word AS is optional, so SELECT price p and SELECT price AS p mean the same thing, but writing AS makes the intent explicit and avoids accidents when a comma is missing.

Basic syntax

-- column alias
SELECT column_name AS alias_name
FROM table_name;

-- table alias
SELECT t.column_name
FROM table_name AS t;

If the alias contains spaces or special characters, or matches a reserved word, wrap it in the identifier quotes your database uses. Standard SQL and PostgreSQL use double quotes, MySQL uses backticks, and SQL Server uses square brackets or double quotes.

SQL AS (alias) examples

Alias a calculated column

Without an alias the computed column has no meaningful name. Naming it line_total makes the output self-explanatory and lets other code refer to it.

SELECT product_name,
       unit_price * units_in_stock AS stock_value
FROM products;

Alias with spaces

When a heading needs spaces, quote the alias. This form is mostly used for reports that go straight to a spreadsheet or a person.

SELECT first_name AS "First Name",
       last_name  AS "Last Name"
FROM customers;

Alias tables in a join

Short aliases keep join queries compact. Each column is qualified with the alias, which avoids ambiguity between the two id-style columns.

SELECT c.first_name, o.order_id, o.order_date
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.customer_id;

Self join with two aliases

To list each employee with their manager, the same table is read twice. The two aliases e and m let the query treat them as separate sources.

SELECT e.first_name AS employee,
       m.first_name AS manager
FROM employees AS e
LEFT JOIN employees AS m ON e.manager_id = m.employee_id;

Where SQL dialects differ

PostgreSQL: Folds unquoted aliases to lower case, so AS FullName is returned as fullname. Use double quotes to keep the case. AS is optional for table aliases and for most column aliases, but required when the alias is a keyword.

MySQL: Accepts single quotes, double quotes or backticks around a column alias, though backticks are the safest choice. An alias may be used in ORDER BY, GROUP BY and HAVING, but not in WHERE.

SQL Server: Supports the older alias = expression form, as in SELECT total = qty * price, as well as AS. An alias can be used in ORDER BY but not in WHERE, GROUP BY or HAVING.

SQLite: Accepts AS with column and table aliases and tolerates quoted strings as aliases for compatibility. Aliases can be used in ORDER BY and GROUP BY.

Oracle: The AS keyword is allowed for column aliases but not for table aliases. Unquoted aliases are converted to upper case, and quoted aliases are limited to 128 bytes in 12.2 and later (30 bytes before that).

Common mistakes

  • Using a column alias in the WHERE clause. The alias does not exist yet when WHERE runs, so repeat the expression or move the query into a subquery.
  • Leaving out a comma between two columns and accidentally creating an alias. SELECT first_name last_name returns one column named last_name in many databases.
  • Choosing an alias that matches a reserved word such as order, group or user, which causes confusing syntax errors unless you quote it.
  • Assuming the alias is stored. It disappears after the query, so a view or application must reference the original column unless the alias is part of a saved definition.
  • Writing AS before a table alias in Oracle, which raises an error. Drop the keyword and use FROM employees e.

Related SQL guides

Browse all SQL guides

Need help writing the SQL?

Describe your tables and the result you want, then review the generated query against your database before running it.

Try the AI SQL generator