SQL aliases make every query you write easier to read and faster to type. The AS keyword lets you give temporary names to columns and tables, making your output cleaner and your multi-table queries far more manageable.
In this guide, we'll cover everything you need to know about SQL aliases with practical examples using the Chinook database (a digital music store with artists, albums, tracks, invoices, and customers).
Table of Contents
- What Is a SQL Alias?
- Column Aliases
- Table Aliases
- Aliases in JOINs
- Aliases with Aggregate Functions
- Cheat Sheet
- Common Mistakes to Avoid
- Practice Exercises
- Summary
- FAQ
What Is a SQL Alias?
A SQL alias is a temporary name you assign to a column or table for the duration of a query. Aliases exist only while the query runs — they don't rename anything permanently in the database.
The basic syntax uses the AS keyword:
-- Column alias
SELECT column_name AS alias_name
FROM table_name;
-- Table alias
SELECT t.column_name
FROM table_name AS t;
Good to know: The
ASkeyword is technically optional in most databases.SELECT Name artist_name FROM artistsworks the same asSELECT Name AS artist_name FROM artists. However, always includeASfor readability — it makes your intent crystal clear.
Column Aliases
Column aliases rename the output columns in your result set. This is useful when the original column name is unclear, when you're computing a value, or when you need a presentation-ready label.
Example: Renaming Columns for Clarity
SELECT
FirstName AS first_name,
LastName AS last_name,
Email AS contact_email
FROM customers
LIMIT 5;
| first_name | last_name | contact_email |
|---|---|---|
| Luís | Gonçalves | luisg@embraer.com.br |
| Leonie | Köhler | leonekohler@surfeu.de |
| François | Tremblay | ftremblay@gmail.com |
| Bjørn | Hansen | bjorn.hansen@yahoo.no |
| František | Wichterlová | frantisekw@jetbrains.com |
Without aliases, the columns would display as FirstName, LastName, and Email — which may not match the lowercase convention your application or report expects.
Example: Aliases for Computed Columns
When you create a calculated column, it has no name by default. Aliases give it one:
SELECT
Name AS track_name,
Milliseconds / 1000 AS duration_seconds,
Bytes / 1048576.0 AS size_mb
FROM tracks
LIMIT 5;
| track_name | duration_seconds | size_mb |
|---|---|---|
| For Those About To Rock (We Salute You) | 343 | 10.97 |
| Balls to the Wall | 342 | 5.26 |
| Fast As a Shark | 230 | 3.86 |
| Restless and Wild | 252 | 4.27 |
| Princess of the Dawn | 375 | 6.29 |
Without the AS duration_seconds alias, the column header would show Milliseconds / 1000 — not exactly user-friendly.
Example: Aliases with String Concatenation
SELECT
FirstName || ' ' || LastName AS full_name,
City || ', ' || Country AS location
FROM customers
LIMIT 5;
| full_name | location |
|---|---|
| Luís Gonçalves | São José dos Campos, Brazil |
| Leonie Köhler | Stuttgart, Germany |
| François Tremblay | Montréal, Canada |
| Bjørn Hansen | Oslo, Norway |
| František Wichterlová | Prague, Czech Republic |
Try it yourself — Practice column aliases with customer data →
Table Aliases
Table aliases give short names to tables, reducing the amount of typing in your queries. They become essential once you start joining multiple tables.
Example: Simple Table Alias
SELECT
t.Name AS track_name,
t.Composer AS composer
FROM tracks AS t
WHERE t.Composer IS NOT NULL
LIMIT 5;
| track_name | composer |
|---|---|
| For Those About To Rock (We Salute You) | Angus Young, Malcolm Young, Brian Johnson |
| Fast As a Shark | F. Baltes, S. Kaufman, U. Dirkschneider |
| Restless and Wild | F. Baltes, R.A. Smith-Diesel, S. Kaufman |
| Princess of the Dawn | Deaffy, R.A. Smith-Diesel |
| Put The Finger On You | Angus Young, Malcolm Young, Brian Johnson |
Here, t is a short alias for tracks. Instead of writing tracks.Name, you write t.Name. The savings add up fast in complex queries.
Common Table Alias Conventions
| Table | Common Alias |
|---|---|
customers | c |
invoices | i |
invoice_items | ii |
tracks | t |
albums | al |
artists | ar |
genres | g |
employees | e |
Pick short, meaningful abbreviations. Single letters work well for simple queries; two-letter abbreviations help when multiple tables start with the same letter (like albums and artists).
Aliases in JOINs
Table aliases truly shine when you're writing JOIN queries. Without them, multi-table queries become painfully verbose.
Example: Without Aliases (Verbose)
SELECT
artists.Name,
albums.Title
FROM artists
INNER JOIN albums ON artists.ArtistId = albums.ArtistId
LIMIT 5;
Example: With Aliases (Clean)
SELECT
ar.Name AS artist_name,
al.Title AS album_title
FROM artists AS ar
INNER JOIN albums AS al ON ar.ArtistId = al.ArtistId
LIMIT 5;
| artist_name | album_title |
|---|---|
| AC/DC | For Those About To Rock We Salute You |
| Accept | Balls to the Wall |
| Accept | Restless and Wild |
| AC/DC | Let There Be Rock |
| Aerosmith | Big Ones |
Both queries return identical results, but the aliased version is shorter, more readable, and much easier to modify.
Example: Multi-Table JOIN with Aliases
When you're joining three or more tables, aliases keep the query manageable:
SELECT
c.FirstName || ' ' || c.LastName AS customer,
i.InvoiceDate AS purchase_date,
t.Name AS track_name
FROM invoices AS i
INNER JOIN customers AS c ON i.CustomerId = c.CustomerId
INNER JOIN invoice_items AS ii ON i.InvoiceId = ii.InvoiceId
INNER JOIN tracks AS t ON ii.TrackId = t.TrackId
LIMIT 5;
| customer | purchase_date | track_name |
|---|---|---|
| Luís Gonçalves | 2021-01-01 | Balls to the Wall |
| Luís Gonçalves | 2021-01-01 | Restless and Wild |
| Leonie Köhler | 2021-01-02 | Put The Finger On You |
| Leonie Köhler | 2021-01-02 | Inject The Venom |
| Leonie Köhler | 2021-01-02 | Evil Walks |
Imagine writing invoice_items.InvoiceId and customers.CustomerId every time — aliases save you from that. For a deeper dive on combining tables, see our complete JOINs guide.
Try it yourself — Practice multi-table JOINs with aliases →
Aliases with Aggregate Functions
Aggregate functions like COUNT(), SUM(), and AVG() produce calculated columns that have no natural name. Aliases are essential here. For more on aggregation, check out our GROUP BY and HAVING guide.
Example: Counting Tracks per Genre
SELECT
g.Name AS genre,
COUNT(t.TrackId) AS track_count
FROM genres AS g
INNER JOIN tracks AS t ON g.GenreId = t.GenreId
GROUP BY g.Name
ORDER BY track_count DESC
LIMIT 5;
| genre | track_count |
|---|---|
| Rock | 1297 |
| Latin | 579 |
| Metal | 374 |
| Alternative & Punk | 332 |
| Jazz | 130 |
Without the AS track_count alias, the column would display as COUNT(t.TrackId) — confusing for anyone reading a report or dashboard.
Example: Revenue Summary with Aliases
SELECT
c.Country AS country,
COUNT(i.InvoiceId) AS total_orders,
ROUND(SUM(i.Total), 2) AS total_revenue,
ROUND(AVG(i.Total), 2) AS avg_order_value
FROM customers AS c
INNER JOIN invoices AS i ON c.CustomerId = i.CustomerId
GROUP BY c.Country
ORDER BY total_revenue DESC
LIMIT 5;
| country | total_orders | total_revenue | avg_order_value |
|---|---|---|---|
| USA | 91 | 523.06 | 5.75 |
| Canada | 56 | 303.96 | 5.43 |
| France | 35 | 195.10 | 5.57 |
| Brazil | 35 | 190.10 | 5.43 |
| Germany | 28 | 156.48 | 5.59 |
Notice how every alias in the SELECT is reused in the ORDER BY clause — ORDER BY total_revenue DESC. This is one of the practical benefits of column aliases: you can reference them in ORDER BY without repeating the expression.
Try it yourself — Practice aggregate queries on the Chinook database →
Cheat Sheet
| What You Want | Syntax | Example |
|---|---|---|
| Rename a column | SELECT col AS alias | SELECT Name AS track_name |
| Name a computed column | SELECT expr AS alias | SELECT Bytes / 1024 AS size_kb |
| Alias with spaces | SELECT col AS "alias name" | SELECT Name AS "Track Name" |
| Shorten a table name | FROM table AS t | FROM tracks AS t |
| Alias in a JOIN | JOIN table AS t ON ... | JOIN albums AS al ON ... |
| Use alias in ORDER BY | ORDER BY alias | ORDER BY track_count DESC |
| Alias in aggregate | SELECT COUNT(*) AS alias | SELECT COUNT(*) AS total |
Common Mistakes to Avoid
1. Using a Column Alias in WHERE
SQL processes the WHERE clause before SELECT, so column aliases don't exist yet at that stage:
-- This will NOT work
SELECT Milliseconds / 1000 AS duration_sec
FROM tracks
WHERE duration_sec > 300;
-- Do this instead: repeat the expression
SELECT Milliseconds / 1000 AS duration_sec
FROM tracks
WHERE Milliseconds / 1000 > 300;
You can use column aliases in ORDER BY and (in most databases) HAVING, but never in WHERE.
2. Forgetting Aliases in Self-Joins
When a table is joined to itself, aliases are required — otherwise the database can't tell the two copies apart:
-- Required: use aliases to distinguish the two copies
SELECT
e.FirstName || ' ' || e.LastName AS employee,
m.FirstName || ' ' || m.LastName AS manager
FROM employees AS e
LEFT JOIN employees AS m ON e.ReportsTo = m.EmployeeId
LIMIT 5;
3. Using Reserved Words as Aliases
Avoid naming your alias something that clashes with SQL keywords like SELECT, FROM, ORDER, GROUP, or TABLE. If you must, wrap it in double quotes: AS "order". Better yet, choose a different name: AS order_id.
4. Inconsistent Alias Conventions
Mixing styles — customer_name in one column and AlbumTitle in another — creates confusing output. Pick a convention (typically snake_case for aliases) and stick with it throughout your query.
Practice Exercises
Put your alias knowledge to work with these hands-on exercises on SQiLs — no setup required, just write SQL in your browser:
| Exercise | What You'll Practice |
|---|---|
| List All Music Artist Names → | Basic SELECT with column aliases |
| Format Customer Names → | String concatenation with aliases |
| Calculate Track Duration in Minutes → | Computed columns with aliases |
| Average Song Length per Genre → | Aggregate functions with table and column aliases |
| Revenue Distribution by Country → | GROUP BY with aliased aggregates |
| Customer Names and Their Invoices → | JOIN queries using table aliases |
Join 500+ SQL learners already practicing on SQiLs — Browse all exercises →
Summary
SQL aliases are a small syntax feature with a big impact on readability and productivity:
- Column aliases (
SELECT col AS alias) rename output columns and are essential for computed or aggregated values. - Table aliases (
FROM table AS t) shorten table references and are critical in JOIN queries. - The
ASkeyword is optional but always recommended for clarity. - Aliases are temporary — they exist only for the duration of the query.
- You can use column aliases in
ORDER BYbut not inWHERE.
Start using aliases in every query you write. Your future self — and anyone reading your SQL — will thank you.
Ready to practice? Start solving SQL exercises on SQiLs →
Frequently Asked Questions
Is the AS keyword required in SQL?
No. In most SQL databases, AS is optional — SELECT Name artist_name works the same as SELECT Name AS artist_name. However, omitting AS can make your query harder to read, especially for table aliases. Best practice is to always include it.
Can I use a column alias in a WHERE clause?
No. SQL evaluates the WHERE clause before SELECT, so the alias doesn't exist yet. You need to repeat the full expression in your WHERE condition. However, you can use column aliases in ORDER BY and, in most databases, in HAVING clauses. See our GROUP BY guide for more on HAVING.
What is the difference between a column alias and a table alias?
A column alias renames a column in the output (SELECT Name AS artist_name), while a table alias gives a table a shorthand reference for the query (FROM artists AS ar). Column aliases affect what you see in results; table aliases affect how you reference columns throughout the query. Both are temporary and exist only for that single query.
When should I use table aliases?
Always use table aliases when writing JOINs — they keep your query concise and readable. For single-table queries, aliases are optional but still helpful if the table name is long. In self-joins (where a table joins to itself), table aliases are required because the database needs a way to tell the two instances apart.



