SQL Tutorial · Chapter 14 of 32
SQL aliases give a column or a table a temporary name that exists only while one query runs. A column alias makes result headings readable and gives calculated columns a proper name; a table alias shortens long table names so joins are easier to write and read. You create an alias with the AS keyword, which is optional in most databases but makes the query clearer.
This lesson from our free SQL series covers the syntax, six worked examples on two small sample tables with their exact results, which clauses can and cannot see an alias, differences between MySQL, SQL Server, PostgreSQL, SQLite and Oracle, and the mistakes beginners make most often.
SQL Alias Syntax
-- column alias
SELECT column_name AS alias_name FROM table_name;
-- table alias
SELECT t.column_name FROM table_name AS t;
-- alias containing a space (must be quoted)
SELECT Amount AS "Order Amount" FROM Orders;
| Part | Meaning |
|---|---|
column_name or expression |
The column or calculation you want to rename in the result. |
AS |
Introduces the alias. Optional for columns; optional for tables except in Oracle, where AS is not allowed before a table alias. |
alias_name |
The temporary name. Quote it if it contains spaces or is a reserved word. |
table_name AS t |
A short name for a table, used as a prefix (t.column) everywhere else in the query. |
Sample Tables Used in This Lesson
Customers
| CustomerID | Name | City | Country |
|---|---|---|---|
| 1 | Anita Sharma | Mumbai | India |
| 2 | John Smith | London | UK |
| 3 | Maria Garcia | Madrid | Spain |
| 4 | Wei Chen | Singapore | Singapore |
| 5 | Emma Brown | Sydney | Australia |
Orders
| OrderID | CustomerID | Product | Amount | OrderDate |
|---|---|---|---|---|
| 101 | 1 | Laptop | 1200 | 2026-01-05 |
| 102 | 2 | Mouse | 25 | 2026-01-07 |
| 103 | 1 | Monitor | 300 | 2026-01-12 |
| 104 | 3 | Keyboard | 45 | 2026-02-02 |
| 105 | 6 | Headset | 80 | 2026-02-15 |
| 106 | 4 | Laptop | 1150 | 2026-03-01 |
Example 1: Rename a Column
SELECT Name AS CustomerName, City AS Location
FROM Customers
WHERE Country = 'UK';
Result: one row with the headings CustomerName | Location and the values John Smith | London. The column in the table is still called Name; only the heading in this result changes.
Example 2: Name a Calculated Column
Without an alias, a calculated column gets an automatic heading such as Amount * 1.18 (MySQL), ?column? (PostgreSQL) or “(No column name)” (SQL Server). An alias fixes that:
SELECT OrderID, Amount, ROUND(Amount * 1.18, 2) AS AmountWithTax
FROM Orders
WHERE Amount > 100;
| OrderID | Amount | AmountWithTax |
|---|---|---|
| 101 | 1200 | 1416.00 |
| 103 | 300 | 354.00 |
| 106 | 1150 | 1357.00 |
Aliases are especially important when a query feeds a report, an API or a spreadsheet, because the alias becomes the field name the other tool sees. See SQL SELECT for more on calculated columns.
Example 3: Table Aliases in a Join
Table aliases keep join queries short. Once you alias a table, use the alias — not the original name — everywhere in that query.
SELECT c.Name, o.Product, o.Amount
FROM Customers AS c
INNER JOIN Orders AS o ON o.CustomerID = c.CustomerID
WHERE o.Amount > 1000;
Result: Anita Sharma | Laptop | 1200 and Wei Chen | Laptop | 1150. Without the c. and o. prefixes, CustomerID would be ambiguous because both tables have it. The SQL INNER JOIN lesson explains the join itself.
Example 4: Alias With Spaces
SELECT Product AS "Product Name", Amount AS "Order Amount"
FROM Orders
WHERE OrderID = 101;
Result: headings Product Name | Order Amount, values Laptop | 1200. Double quotes are the SQL standard. MySQL also accepts backticks (`Order Amount`) and SQL Server accepts square brackets ([Order Amount]). Single quotes also work as alias delimiters in MySQL and SQL Server, but PostgreSQL rejects them, so avoid them for portable code.
Example 5: Aliases With Aggregates and ORDER BY
SELECT c.Country,
COUNT(o.OrderID) AS OrderCount,
SUM(o.Amount) AS TotalRevenue
FROM Customers AS c
INNER JOIN Orders AS o ON o.CustomerID = c.CustomerID
GROUP BY c.Country
ORDER BY TotalRevenue DESC;
| Country | OrderCount | TotalRevenue |
|---|---|---|
| India | 2 | 1500 |
| Singapore | 1 | 1150 |
| Spain | 1 | 45 |
| UK | 1 | 25 |
ORDER BY runs after SELECT, so it can sort by the alias TotalRevenue directly. Order 105 drops out because CustomerID 6 has no customer row, and Australia drops out because Emma Brown has no orders. More on grouping in SQL GROUP BY.
Example 6: Filtering on an Alias With a Subquery
A column alias cannot be used in WHERE (see the next section). The standard workaround is to wrap the query in a subquery — which itself needs a table alias:
SELECT t.Product, t.WithTax
FROM (
SELECT Product, Amount * 1.18 AS WithTax
FROM Orders
) AS t
WHERE t.WithTax > 1000;
Result: Laptop | 1416 and Laptop | 1357. MySQL and SQL Server require an alias on a derived table like this (PostgreSQL did too before version 16); leaving out AS t raises an error there. See SQL Subqueries for more patterns.
Bonus: A Self Join Needs Two Aliases
When a table is joined to itself, aliases are the only way to tell the two copies apart. This finds customers who placed more than one order:
SELECT o1.CustomerID, o1.OrderID AS FirstOrder, o2.OrderID AS LaterOrder
FROM Orders AS o1
JOIN Orders AS o2
ON o1.CustomerID = o2.CustomerID
AND o1.OrderID < o2.OrderID;
Result: 1 | 101 | 103 — Anita Sharma is the only repeat customer in the sample.
Where Can a Column Alias Be Used?
| Clause | Column alias allowed? | Why |
|---|---|---|
| ORDER BY | Yes, in all major databases | Runs after SELECT |
| WHERE | No | Runs before SELECT |
| GROUP BY | MySQL, PostgreSQL, SQLite: yes. SQL Server: no | Vendor extension |
| HAVING | MySQL, SQLite: yes. PostgreSQL, SQL Server: no — repeat the aggregate | Vendor extension |
The safest habit: use aliases freely in SELECT and ORDER BY, and repeat the expression elsewhere. The SQL HAVING lesson shows the repeated-aggregate style.
Database Differences
- AS is optional for column and table aliases in MySQL, SQL Server, PostgreSQL and SQLite.
- Oracle accepts AS for column aliases but not for table aliases — write
FROM Customers c, notFROM Customers AS c. Leaving AS off table aliases is therefore the most portable habit. - SQL Server also supports the non-standard form
SELECT AmountWithTax = Amount * 1.18. - Row limits differ: if you add
LIMIT 2to Example 5 it works in MySQL, PostgreSQL and SQLite; SQL Server usesSELECT TOP 2instead.
Common Mistakes and How to Fix Them
- Using a column alias in WHERE.
WHERE WithTax > 1000fails with an “unknown column” or “invalid column name” error. WriteWHERE Amount * 1.18 > 1000or use a subquery as in Example 6. - Using the original table name after aliasing it. In
FROM Customers AS c, writingCustomers.Namefails in most databases. Usec.Name. - Forgetting a comma.
SELECT Name City FROM Customersreturns one column called City that contains customer names, because City became an alias for Name. - Reusing a real column name for a different value. Aliasing
Amount * 1.18 AS Amountmakes ORDER BY and later edits confusing. Pick a new name. - Quoting an alias with single quotes in PostgreSQL. It raises a syntax error. Use double quotes for aliases that need quoting.
Naming Tips
- Make column aliases read like headings:
TotalRevenue,OrderCount,AvgAmount. - Avoid spaces so you never need quotes, unless the result is going straight into a report.
- Use short, consistent table aliases across a project —
cfor Customers,ofor Orders — so any query is easy to scan.
Practice Exercise
- Return City renamed to Town and Country renamed to Nation for all customers.
- Using table aliases c and o, list each customer name with their OrderID for orders placed in January 2026.
Answers:
-- 1
SELECT City AS Town, Country AS Nation FROM Customers;
-- 2
SELECT c.Name, o.OrderID
FROM Customers c
JOIN Orders o ON o.CustomerID = c.CustomerID
WHERE o.OrderDate BETWEEN '2026-01-01' AND '2026-01-31';
-- Anita Sharma 101, John Smith 102, Anita Sharma 103
Renaming and labelling columns is a core habit in spreadsheet reporting too. The Excel Pivot Tables & Dashboards course on NextGenTemplates Academy shows how to turn raw columns into clear report fields, with practice files.
Frequently Asked Questions
Is the AS keyword required for SQL aliases?
No. SELECT Name CustomerName works in every major database. AS just makes the alias easier to spot. The one restriction is Oracle, which does not allow AS before a table alias.
Can I use a column alias in the WHERE clause?
No, because WHERE is evaluated before the SELECT list. Repeat the expression in WHERE or wrap the query in a subquery and filter on the alias outside it.
Do aliases rename the table or column permanently?
No. An alias exists only for the query in which it is written. To rename a column permanently, use ALTER TABLE ... RENAME COLUMN.
When must I use a table alias?
When you join a table to itself (a self join) and when you select from a subquery in MySQL or SQL Server (and in PostgreSQL before version 16). In other cases aliases are optional but make queries much easier to read.
How do I put a space in an alias?
Wrap it in double quotes: AS "Order Amount". MySQL also accepts backticks and SQL Server accepts square brackets.
Wrapping Up
Column aliases make results readable and give calculated values proper names; table aliases keep joins short and make self joins possible. Remember that WHERE cannot see a column alias, and you will avoid the most common alias error. Next, put table aliases to work in the SQL Joins introduction.
📅 Last updated: October 2026
Chapter 14 of 32 · SQL from Scratch: all 32 chapters



