Skip to content
Laviyx Learn

Try

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.

Beginner7 min readUpdated
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#

idnamedepartmentsalarycity
1AshaSales52000Mumbai
2RaviEngineering78000Bengaluru
3MeeraEngineering81000Pune
4JohnMarketing46000Mumbai
5PriyaSales61000Delhi

Column aliases#

Put AS and a new name after a column or expression:

SQLRun
SELECT name AS employee_name,
       salary AS monthly_salary
FROM employees
WHERE department = 'Sales';
Output
employee_name | monthly_salary
--------------+---------------
Asha          | 52000
Priya         | 61000

Aliases 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.

SQLRun
SELECT name, salary * 0.10 AS bonus
FROM employees
ORDER BY bonus DESC
LIMIT 3;
Output
name  | bonus
------+------
Meera | 8100
Ravi  | 7800
Priya | 6100

Aliases with spaces#

If an alias contains spaces or capital letters you want to keep, wrap it in double quotes:

SQLRun
SELECT name, salary * 12 AS "Yearly Salary"
FROM employees
ORDER BY "Yearly Salary" DESC
LIMIT 2;
Output
name  | Yearly Salary
------+--------------
Meera | 972000
Ravi  | 936000

The 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:

SQLRun
SELECT e.name, e.city
FROM employees AS e
WHERE e.salary > 60000;
Output
name  | city
------+----------
Ravi  | Bengaluru
Meera | Pune
Priya | Delhi

With one table this only saves a little typing. With JOINs, table aliases become essential, because several tables can have columns with the same name:

SQLRun
SELECT c.name, o.amount
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
ORDER BY o.amount DESC;
Output
name | amount
-----+-------
Ravi | 560
Asha | 250
Asha | 120

Here 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:

SQLRun
-- 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:

SQLRun
SELECT name, salary * 12 AS yearly
FROM employees
WHERE salary * 12 > 700000;
Output
name  | yearly
------+-------
Ravi  | 936000
Meera | 972000
Priya | 732000

Common 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
SQLRun
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;
Output
employee | new_salary
---------+-----------
Meera    | 85050
Ravi     | 81900
Priya    | 64050

Some databases show these as 85050.00 because multiplying by 1.05 produces a decimal.

Key takeaway#

Finished this lesson?