SQL Aliases
On this page
SQL Aliases
Aliases are temporary names given to columns or tables in a query. They improve readability and make complex queries easier to write and understand.
Column Alias Syntax
SELECT column_name AS alias_name FROM table_name;
Example: Column Alias
SELECT name AS customer_name,
email AS contact_email
FROM customers;
Alias Without AS
The AS keyword is optional in many database systems.
SELECT name customer_name FROM customers;
However, using AS improves clarity and is recommended.
Alias with Expressions
Aliases are especially useful when working with computed columns.
SELECT price * quantity AS total_price FROM order_items;
Using Alias in ORDER BY
You can reference a column alias in ORDER BY.
SELECT price * quantity AS total_price FROM order_items ORDER BY total_price DESC;
Table Aliases
Table aliases are useful when joining multiple tables or when writing long table names.
SELECT c.name, o.order_date FROM customers AS c JOIN orders AS o ON c.id = o.customer_id;
Why Table Aliases Matter
- Shorter queries
- Clear distinction between columns from different tables
- Required when the same table is used multiple times (self join)
Common Mistakes
- Forgetting to use the alias consistently
- Using unclear alias names like a, b, c in complex queries
- Confusing column aliases with actual table column names
Next Step
Continue with SQL Joins to learn how to combine data from multiple tables.