When you write SQL queries, the column names in your result often come straight from the table definition. Names such as unit_price or order_date are useful in the database, but they may look technical in a report, or they may collide when you join two tables that both contain a column called id. SQL solves this with aliases: temporary names that you assign to columns or tables for the duration of a single query.
In this tutorial, you will learn what an alias is, how to create one with the AS keyword, how aliases make joins easier to read, and which pitfalls to avoid. By the end, you will be able to produce cleaner result sets and shorter, more maintainable queries.
Prerequisites
To follow along, you need:
- A running database such as PostgreSQL, MySQL, MariaDB, SQL Server, Oracle Database, or SQLite.
- A SQL client or command-line tool to execute statements.
- Basic familiarity with
SELECTandFROM.
If you have not created a practice database yet, any empty schema will do. The setup script below creates everything else.
Sample Database
The examples use a small online bookstore with two tables: authors and books. Each book is written by one author, so books.author_id refers to authors.author_id.
CREATE TABLE authors (
author_id INTEGER PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
country VARCHAR(50)
);
CREATE TABLE books (
book_id INTEGER PRIMARY KEY,
title VARCHAR(200) NOT NULL,
author_id INTEGER NOT NULL,
unit_price DECIMAL(8, 2) NOT NULL,
published_on DATE,
CONSTRAINT fk_books_author
FOREIGN KEY (author_id) REFERENCES authors (author_id)
);
INSERT INTO authors (author_id, first_name, last_name, country) VALUES
(1, 'Joshua', 'Bloch', 'USA'),
(2, 'Martin', 'Fowler', 'UK'),
(3, 'Robert', 'Martin', 'USA');
INSERT INTO books (book_id, title, author_id, unit_price, published_on) VALUES
(101, 'Effective Java', 1, 45.00, DATE '2018-01-06'),
(102, 'Refactoring', 2, 50.00, DATE '2018-11-20'),
(103, 'Clean Code', 3, 40.00, DATE '2008-08-01'),
(104, 'Clean Architecture', 3, 42.00, DATE '2017-09-10');
Note:
DATE '2018-01-06'is ANSI-standard syntax. If your database does not accept it, use a plain string literal such as'2018-01-06'.
Basic Syntax
There are two kinds of aliases: column aliases and table aliases.
Column alias:
SELECT column_name AS alias_name
FROM table_name;
Table alias:
SELECT alias_name.column_name
FROM table_name AS alias_name;
The AS keyword is optional in most databases; first_name AS given_name and first_name given_name mean the same thing. Writing AS explicitly is recommended because it is easier to read and harder to confuse with a missing comma.
Practical Example: Renaming Columns
Suppose you want a report with a friendlier heading than first_name and last_name.
SELECT
first_name AS given_name,
last_name AS family_name,
country AS home_country
FROM authors;
What this query does:
FROM authorsselects rows from theauthorstable.SELECTpicks three columns and renames them for the result set.- The database returns rows using the new headings.
Expected Result
| given_name | family_name | home_country |
|---|---|---|
| Joshua | Bloch | USA |
| Martin | Fowler | UK |
| Robert | Martin | USA |
The underlying column names in the authors table are unchanged. An alias exists only for the current query.
Practical Example: Aliasing Computed Columns
Aliases are especially useful for expressions, because computed columns otherwise get awkward, database-generated names.
SELECT
title,
unit_price,
unit_price * 0.9 AS discounted_price
FROM books;
The expression unit_price * 0.9 has no natural name. The alias discounted_price gives the result column a clear meaning.
Expected Result
| title | unit_price | discounted_price |
|---|---|---|
| Effective Java | 45.00 | 40.50 |
| Refactoring | 50.00 | 45.00 |
| Clean Code | 40.00 | 36.00 |
| Clean Architecture | 42.00 | 37.80 |
Practical Example: Table Aliases in Joins
Table aliases become essential when a query references the same column name in more than one table, or when the table names are long.
SELECT
b.title,
b.unit_price,
a.first_name AS author_first_name,
a.last_name AS author_last_name
FROM books AS b
INNER JOIN authors AS a
ON a.author_id = b.author_id
ORDER BY
b.title;
What this query does:
FROM books AS bgives thebookstable the short aliasb.INNER JOIN authors AS ajoinsauthorsusing the aliasa.ON a.author_id = b.author_idmatches each book with its author.- The
SELECTlist uses qualified names such asb.titleso the database knows which table each column belongs to. ORDER BY b.titlesorts the final result by book title.
Expected Result
| title | unit_price | author_first_name | author_last_name |
|---|---|---|---|
| Clean Architecture | 42.00 | Robert | Martin |
| Clean Code | 40.00 | Robert | Martin |
| Effective Java | 45.00 | Joshua | Bloch |
| Refactoring | 50.00 | Martin | Fowler |
Without aliases, you would repeat books. and authors. everywhere, making the query harder to scan.
Practical Example: Aliases with Spaces or Mixed Case
If you want the alias to contain spaces or preserve capitalization, you must quote it. Standard SQL uses double quotes; SQL Server and Sybase also accept square brackets; MySQL and MariaDB additionally accept backticks.
SELECT
first_name AS "Given Name",
last_name AS "Family Name"
FROM authors;
Use this feature sparingly. Quoted aliases with spaces are useful for reports but inconvenient for application code that must reference the column by name.
Common Mistakes
Using a Column Alias in the WHERE Clause
Column aliases are assigned in the SELECT list, which is processed after WHERE. Referring to an alias in WHERE therefore fails in most databases.
Incorrect:
SELECT
unit_price * 0.9 AS discounted_price
FROM books
WHERE discounted_price < 40;
Correct:
SELECT
unit_price * 0.9 AS discounted_price
FROM books
WHERE unit_price * 0.9 < 40;
You can, however, reference a column alias in ORDER BY, because sorting happens after the SELECT list is evaluated.
Forgetting to Qualify Columns After Adding a Table Alias
Once you assign a table alias, some databases require you to use the alias instead of the original table name for the rest of the query.
Incorrect:
SELECT books.title
FROM books AS b;
Correct:
SELECT b.title
FROM books AS b;
Forgetting the Comma Between Columns
A missing comma turns the next column name into an alias.
SELECT
first_name
last_name
FROM authors;
This query returns a single column, first_name, aliased as last_name. Always double-check commas.
Database Compatibility
| Feature | PostgreSQL | MySQL | MariaDB | SQL Server | Oracle Database | SQLite |
|---|---|---|---|---|---|---|
AS keyword for column aliases |
Yes | Yes | Yes | Yes | Yes | Yes |
AS keyword for table aliases |
Yes | Yes | Yes | Yes | No (must omit AS) |
Yes |
| Double-quoted aliases | Yes | Yes (in ANSI mode) | Yes (in ANSI mode) | Yes | Yes | Yes |
| Square-bracket aliases | No | No | No | Yes | No | Yes |
| Backtick aliases | No | Yes | Yes | No | No | Yes |
Oracle Database is the notable exception: it does not accept AS in front of a table alias. Write FROM books b, not FROM books AS b.
Best Practices
- Prefer descriptive aliases such as
order_countover cryptic ones such asoc. - Keep table aliases short but meaningful.
cforcustomersandoforordersare common conventions. - Always write
ASfor column aliases; it improves readability. - Qualify every column with its table alias in multi-table queries, even when the column name is unambiguous today. Future schema changes may introduce a conflict.
- Avoid aliases that shadow existing column or table names.
- Do not depend on aliases to hide poor column names. If a column is consistently misnamed, consider renaming it in the schema instead.
Conclusion
You learned how to use SQL aliases to rename columns and tables inside a query. Column aliases give result sets readable headings and label computed expressions; table aliases keep join queries short and unambiguous. Remember that aliases exist only for the duration of the query and that column aliases cannot be used in WHERE.
