SQL course menu
SQL Aliases: Rename Columns and Tables With AS
Learn SQL aliases: give columns and calculations readable names with AS, shorten table names in joins, quote aliases with spaces, and understand where aliases can and cannot be used.
On this page
An alias is a temporary name you give to a column or a table inside one query. Aliases make results easier to read and long queries easier to write. They do not change anything in the database.
The example table#
Column aliases#
Put AS and a new name after a column or expression:
SELECT name AS employee_name,
salary AS monthly_salary
FROM employees
WHERE department = 'Sales';employee_name | monthly_salary
--------------+---------------
Asha | 52000
Priya | 61000Aliases matter most for calculations. Without one, the database invents a column name such as salary * 12 or ?column?, which is awkward to read and to use from application code.
SELECT name, salary * 0.10 AS bonus
FROM employees
ORDER BY bonus DESC
LIMIT 3;name | bonus
------+------
Meera | 8100
Ravi | 7800
Priya | 6100Aliases with spaces#
If an alias contains spaces or capital letters you want to keep, wrap it in double quotes:
SELECT name, salary * 12 AS "Yearly Salary"
FROM employees
ORDER BY "Yearly Salary" DESC
LIMIT 2;name | Yearly Salary
------+--------------
Meera | 972000
Ravi | 936000The AS keyword is optional#
SELECT salary * 12 yearly works the same as SELECT salary * 12 AS yearly. Writing AS is clearer and avoids a classic bug: if you forget a comma between two columns, SELECT name salary silently becomes "the name column, aliased as salary".
Table aliases#
You can also give a table a short name, then use it to prefix columns:
SELECT e.name, e.city
FROM employees AS e
WHERE e.salary > 60000;name | city
------+----------
Ravi | Bengaluru
Meera | Pune
Priya | DelhiWith one table this only saves a little typing. With JOINs, table aliases become essential, because several tables can have columns with the same name:
SELECT c.name, o.amount
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
ORDER BY o.amount DESC;name | amount
-----+-------
Ravi | 560
Asha | 250
Asha | 120Here c and o make it obvious which table each column comes from. Oracle does not allow AS before a table alias, so writing customers c works everywhere.
Where can you use a column alias?#
Aliases are created in the SELECT step. Recall the order SQL processes a query:
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
So an alias can be used in ORDER BY (which runs after SELECT), but cannot be used in WHERE (which runs before it) in most databases:
-- Error in PostgreSQL, SQL Server and Oracle: column "yearly" does not exist
SELECT name, salary * 12 AS yearly
FROM employees
WHERE yearly > 700000;Repeat the expression instead:
SELECT name, salary * 12 AS yearly
FROM employees
WHERE salary * 12 > 700000;name | yearly
------+-------
Ravi | 936000
Meera | 972000
Priya | 732000Common mistakes#
Practice#
Practice exercise
Using a table alias, return each employee's name as employee, and their salary plus a 5% raise as new_salary, for employees whose new salary would be over 60,000. Sort by new_salary, highest first.
Show hint
You cannot use new_salary in WHERE, so repeat the calculation there. You can use it in ORDER BY.
Show solution
SELECT e.name AS employee,
e.salary * 1.05 AS new_salary
FROM employees e
WHERE e.salary * 1.05 > 60000
ORDER BY new_salary DESC;employee | new_salary
---------+-----------
Meera | 85050
Ravi | 81900
Priya | 64050Some databases show these as 85050.00 because multiplying by 1.05 produces a decimal.
Key takeaway#
Finished this lesson?