Back to all posts

SQL Aliases Explained with Examples

For someone learning SQL who can read a SELECT and a JOIN; every alias below is shown with the rows it returns.

On this page

An alias gives a result column or a table another name, for one query only. For example, you can give a calculated column a readable heading, or shorten a table name to one letter.

Both kinds of alias have the same shape:

SELECT expression AS column_alias
FROM table_name AS table_alias;
  • expression is a column, a calculation, or an aggregate such as COUNT(*).
  • column_alias is the heading of that column in the result.
  • table_alias is the name that the rest of the query uses for the table.
  • AS is optional before both kinds of alias on PostgreSQL, MySQL and SQL Server.

Create a sample table of four employees:

CREATE TABLE employees (
  emp_id     int PRIMARY KEY,
  name       varchar(20),
  manager_id int,
  salary     int
);

INSERT INTO employees VALUES
  (1, 'Alice', NULL, 5000),
  (2, 'Bob',   1,    4000),
  (3, 'Carol', 1,    4200),
  (4, 'Dan',   2,    3500);

Alice manages Bob and Carol, Bob manages Dan, and Alice has no manager. Every statement below was run on PostgreSQL 18.6 and MySQL 9.1.0, and both returned the same rows. Where the two engines differ, both results are shown.

Column aliases

A column alias sets the heading of a column in the result. For example, you can show the salary column under the heading monthly_salary.

SELECT name AS employee, salary AS monthly_salary
FROM employees;
employeemonthly_salary
Alice5000
Bob4000
Carol4200
Dan3500

The headings employee and monthly_salary come from the aliases. In the employees table, the columns are still called name and salary.

Practice this: Lesson 2 of the SQL tutorial runs SELECT, AS and DISTINCT on a shop database in your browser, with exercises.

Calculated columns and aggregates

A column alias gives a calculation, such as salary * 12, a heading of its own. For example, you can head the yearly pay as yearly_pay.

SELECT name, salary * 12 AS yearly_pay
FROM employees;
nameyearly_pay
Alice60000
Bob48000
Carol50400
Dan42000

Each row shows twelve times the monthly salary, under the heading yearly_pay. Without an alias, PostgreSQL and MySQL each make up a heading for a calculation or an aggregate, and the headings differ:

ExpressionPostgreSQL 18 headingMySQL 9.1 heading
salary * 12?column?salary * 12
COUNT(*)countCOUNT(*)
SUM(salary)sumSUM(salary)

Give every calculated column an alias. An application reads each value by its heading, and the heading that the engine makes up differs from one engine to the next.

Table aliases

A table alias gives a table a short name for the rest of the query. For example, you can call the employees table e, and write e.name for its name column.

SELECT e.name, e.salary
FROM employees AS e;
namesalary
Alice5000
Bob4000
Carol4200
Dan3500

The query returns every employee with their salary. With one table in the query, the alias in front of each column is optional. The alias starts to matter when you join a second table, or the same table a second time.

Self-joins

A self-join matches the rows of a table with other rows of the same table, so the table appears twice, under two aliases. For example, you can list each employee next to their manager's name, which is in the same table.

SELECT e.name AS employee, m.name AS manager
FROM employees AS e
JOIN employees AS m ON e.manager_id = m.emp_id
ORDER BY e.name;
employeemanager
BobAlice
CarolAlice
DanBob

The alias e stands for the employee, and m for the manager. Bob's manager_id is 1, Alice's emp_id, so Bob's row shows Alice. Alice's manager_id is NULL, and NULL equals no emp_id, so Alice has no row in the result.

Both copies of the table have a name column, so a bare name in the select list fails. PostgreSQL reports column reference "name" is ambiguous, and MySQL reports Column 'name' in field list is ambiguous. The column aliases employee and manager give the two name columns different headings in the result.

Aliases in WHERE, GROUP BY, HAVING and ORDER BY

ORDER BY can sort on a column alias. For example, you can sort the employees by yearly_pay, from highest to lowest.

SELECT name, salary * 12 AS yearly_pay
FROM employees
ORDER BY yearly_pay DESC;
nameyearly_pay
Alice60000
Carol50400
Bob48000
Dan42000

Alice comes first with 60000, and Dan last with 42000. The sort can use yearly_pay because the engine sorts the rows after it computes the select list, the columns after SELECT.

The engine handles the clauses of a SELECT in a fixed order. A clause can use an alias once the clause that defines it has run:

The order in which the engine handles the clauses of a SELECT: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. A table alias is defined in FROM, a column alias in SELECT, and GROUP BY and HAVING differ by engine

A table alias comes from FROM, so every clause can use it. A column alias comes from the select list, and WHERE runs before the select list, so WHERE cannot use it. This query fails:

SELECT name, salary * 12 AS yearly_pay
FROM employees
WHERE yearly_pay > 50000;

PostgreSQL reports column "yearly_pay" does not exist, and MySQL reports Unknown column 'yearly_pay' in 'where clause'. Repeat the expression in WHERE, as in WHERE salary * 12 > 50000. You can also filter from an outer query, as under Subqueries in FROM below.

After WHERE, the engines differ:

Column alias inPostgreSQLMySQLSQL Server
WHERENoNoNo
GROUP BYYesYesNo
HAVINGNoYesNo
ORDER BYYesYesYes

PostgreSQL and MySQL accept a column alias in GROUP BY, although GROUP BY runs before SELECT, and MySQL accepts it in HAVING too. Each PostgreSQL and MySQL value was run, and agrees with the documentation of PostgreSQL 18 and MySQL. The SQL Server column comes from Microsoft's documentation, without a run.

Subqueries in FROM

A table alias names a subquery in FROM, and the outer query reads the subquery's rows like a table's. For example, you can compute yearly_pay in a subquery, and filter on it in the outer query.

SELECT name, yearly_pay
FROM (SELECT name, salary * 12 AS yearly_pay FROM employees) AS y
WHERE yearly_pay > 50000
ORDER BY yearly_pay DESC;
nameyearly_pay
Alice60000
Carol50400

The subquery computes yearly_pay for all four employees. The outer WHERE runs after the subquery, so yearly_pay exists there. WHERE keeps Alice and Carol, whose yearly pay is over 50000.

MySQL requires the alias. Without AS y, it stops with ERROR 1248 (42000): Every derived table must have its own alias. PostgreSQL 18 runs the query without the alias, although the SQL standard requires one.

Quoting an alias

Quotes let an alias contain a space, and on PostgreSQL they keep its capital letters. For example, you can head the name column Employee Name. Each engine quotes with different characters:

-- PostgreSQL, and SQL Server with QUOTED_IDENTIFIER ON
SELECT name AS "Employee Name"
FROM employees;

-- MySQL
SELECT name AS `Employee Name`
FROM employees;

-- SQL Server, in any session
SELECT name AS [Employee Name]
FROM employees;
Employee Name
Alice
Bob
Carol
Dan

The four names come back under the heading Employee Name. Unquoted, PostgreSQL folds an alias to lower case, so AS Total comes back as total. SQL Server accepts brackets in every session, and double quotes when QUOTED_IDENTIFIER is on, the default for most connections.

In MySQL, use backticks in ORDER BY too. Outside the select list, MySQL reads "Employee Name" as a string constant, so ORDER BY "Employee Name" DESC leaves the rows unsorted: Alice, Bob, Carol, Dan. PostgreSQL returns Dan, Carol, Bob, Alice.

Quote an alias only when it needs quotes. An alias of lower-case letters, digits and underscores, starting with a letter, comes back the same from every engine.

What goes wrong

A missing comma

SELECT name salary
FROM employees;
salary
Alice
Bob
Carol
Dan

The query was meant to return two columns, name and salary. Without the comma, both engines read salary as an alias for name, and return one column of names headed salary.

Write AS before every alias. A name with no AS before it then stands out as a missing comma.

The table name after a table alias

Once a table has an alias, the rest of the query has to use the alias. Write employees.name in a query on employees AS e, and PostgreSQL stops:

ERROR:  invalid reference to FROM-clause entry for table "employees"
HINT:  Perhaps you meant to reference the table alias "e".

MySQL reports Unknown column 'employees.name' in 'field list'.

A column alias in HAVING

SELECT manager_id, COUNT(*) AS reports
FROM employees
GROUP BY manager_id
HAVING reports > 1;

MySQL returns manager_id 1, Alice, the one manager with more than one report:

manager_idreports
12

PostgreSQL stops with column "reports" does not exist. Write HAVING COUNT(*) > 1, and both engines return that row.

Aliases in DbSchema

DbSchema's Query Builder writes the SELECT for you. You join tables through their foreign keys and tick the columns, and DbSchema gives each table a short alias. In the screenshot below, from a sample project, DbSchema named the tasks table t and the users table u.

DbSchema's Query Builder: the SQL at the right shows the generated SELECT with the table aliases t and u, and the result grid below the join canvas holds the 31 rows returned

To run a query that you wrote, use the SQL Editor. Press Editor in the toolbar, choose SQL Editor, paste the query, and press Run. The result grid under the editor takes its headings from your aliases.

The self-join query in the DbSchema SQL Editor, with the result grid headed employee and manager, the headings taken from the column aliases

Download DbSchema at https://dbschema.com/download.html and connect it to PostgreSQL or MySQL. Paste the CREATE TABLE and INSERT statements into the SQL Editor, then run each query and change its aliases. The free Community Edition includes the database connection and the SQL Editor; the Query Builder needs DbSchema Pro.

Run these aliases against your own schema

DbSchema connects to your database and runs hand-written queries in the SQL Editor, which is part of the free Community Edition. The Query Builder, which builds the SELECT and assigns the table aliases for you, is a Pro feature.