Skip to content
Tutorials Center
My learning

Topics

SQL Reference ยท Statements and Clauses

SQL AS (aliases)

Reference for the SQL AS keyword: give columns and tables temporary names, use aliases in ORDER BY and self joins, quoting rules and dialect differences.

AS gives something a temporary name for the duration of one query. A column alias renames a column or expression in the result. A table alias gives a table, view or subquery a shorter name that you use in the rest of the query.

Aliases make results readable (price * quantity AS line_total), shorten long table names, and are required when the same table appears twice, as in a self join.

SQL
SELECT p.name AS product, p.price * 2 AS double_price
FROM products AS p;

Try it

SQL Column and table aliases ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

SQL Two aliases for one table ยท database: company

Press Run (or Ctrl + Enter) to see the result here.

Syntax

SQL
SELECT expression [AS] column_alias
FROM table_name [AS] table_alias;
Part Description
column_alias A name for the output column. Quote it with "..." if it contains spaces or is a reserved word.
table_alias A name for a table, view or subquery. After you alias a table, refer to it only by the alias.
AS Optional in most dialects but recommended for readability.

Notes: an alias exists only inside the query. It does not rename anything in the database.

Dialect differences

Behavior SQLite PostgreSQL MySQL SQL Server Oracle
AS for columns Optional Optional Optional Optional Optional
AS for tables Optional Optional Optional Optional Not allowed
Quote style for spaces "x", [x], `x` "x" `x` [x] or "x" "x"
Alias folded to lowercase Keeps case Yes, unless quoted Keeps case Keeps case Uppercase, unless quoted
SQL
-- Oracle: table alias without AS
SELECT p.name FROM products p;

PostgreSQL also needs AS for a column alias that is a keyword such as end; always writing AS avoids that trap.

Usage notes

  • Where an alias is visible. Column aliases can be used in ORDER BY. In WHERE, GROUP BY and HAVING they are not portable (MySQL, SQLite and PostgreSQL allow some uses). Repeat the expression or use a subquery.
  • Forgetting a comma before an unaliased column can turn it into an alias by accident: SELECT name price FROM products returns one column called price containing names. Writing AS makes the mistake easier to see.
  • Derived tables need an alias in PostgreSQL, MySQL and SQL Server.
  • Use meaningful aliases: c for customers is fine, but t1, t2 in a long query hides what is going on.
  • Snake case (line_total) avoids quoting. Spaces and capitals force quotes everywhere you use the name.