Cours
A SQL alias is a temporary name for a table or column in an SQL query. It is usually used to simplify a complex query by making it easily readable or simply giving a column a more human-readable or natural-sounding name. In this article, I will discuss the different techniques and practical applications of using an SQL alias.
Before we get started, I recommend taking DataCamp’s Introduction to SQL course to gain foundational knowledge in SQL. The SQL Basics Cheat Sheet will also help you refresh basic SQL concepts during your learning.
What is a SQL Alias?
A SQL alias improves the readability of a query. Assume you have two tables, Products
and Suppliers
.
Example of Products table to rename columns using SQL alias. Image by Author.
Example of Suppliers table to rename columns using SQL alias. Image by Author.
The following query uses the AS
keyword to create an alias, which renames a column in the Products
table.
-- Select the product_name column from the Products table and alias it as 'Product'
-- Select the price column from the Products table and alias it as 'Price'
SELECT product_name AS Product,
price AS Price
FROM Products;
Using the AS Keyword to Create a SQL Alias
SQL aliases are important when renaming long column names or joining multiple tables. The SQL alias syntax uses the AS
keyword to rename the column or table. However, the AS
keyword is optional, provided you indicate the table or column alias.
What is convenient is that different SQL databases, including MySQL, PostgreSQL, and SQL Server, fully support the SQL alias for table and column names, so you don’t need to memorize different database-specific methods.
Example of SQL column alias
The syntax below shows the SQL column alias using the AS
keyword.
SELECT column_name AS alias_name
FROM table_name;
The query below produces the same output because the AS
keyword is optional.
SELECT column_name alias_name
FROM table_name;
Example of a SQL table alias
The syntax below shows the SQL table alias using the AS
keyword.
SELECT column1, column2
FROM table_name AS alias_name;
The following syntax shows the table alias without the AS
keyword.
SELECT column1, column2
FROM table_name alias_name;
When to Use a SQL Alias
The SQL alias has the following applications.
- Making Reports and Dashboards More Clear : Aliases are used to rename columns to include meaningful names, improving understanding of reports.
- Helping the Coding Process: A SQL alias is especially useful when merging columns in tables to improve data clarity.
- Enhancing Collaborative Projects: Using a SQL alias improves collaboration in a team because it helps create consistent naming conventions of tables and columns in a database.
I recommend taking DataCamp’s Reporting in SQL course to learn more about building dashboards and incorporating SQL aliases to create interactive reports. I also recommend checking out our SQL Fundamentals skill track to learn advanced SQL methods, including joining data in more advanced ways.
SQL Alias Examples and Use Cases
The following examples show how to use SQL aliases to rename columns and tables in queries.
Using a SQL alias for column names
The SQL alias can be used to rename column names. Using aliases in columns improves query readability by making complex queries clear. In the example below, SQL aliases have been used to rename two columns: product_name
has become Product
, and price
has become Price
.
-- Select and alias columns
SELECT product_name AS Product,
price AS Price
FROM Products;
Example output of SQL column alias. Image by Author.
Using a SQL alias for table names
The SQL alias can also be used to temporarily rename tables. This technique simplifies complex queries when working with multiple tables. The query below shows how to use table alias when joining two tables. The table aliases improve query readability by shortening the table names. This is really useful when joining two tables together.
-- Select product names and supplier names
SELECT p.product_name,
s.supplier_name
-- From the Products table aliased as 'p'
FROM Products AS p
-- Join with the Suppliers table aliased as 's' on matching supplier_id
JOIN Suppliers AS s ON p.supplier_id = s.supplier_id;
Example output of SQL table alias. Image by Author.
Adhere to the following best practices when using table aliases in complex joins and subqueries. First, use short and descriptive aliases to improve clarity and readability. Second, use different names for columns and tables to avoid name conflicts and ambiguity.
Applying more than one SQL alias
You can combine SQL aliases for column and table names in a single query. This technique is helpful for subqueries and other complex queries. The query below uses a SQL alias for both columns and tables. It will retrieve the product_name
and supplier_name
for those located in New York City.
-- Select product names from the Products table and alias as 'Product'
-- Select supplier names from the subquery and alias as 'Supplier'
SELECT p.product_name AS Product,
s.supplier_name AS Supplier
-- From the Products table aliased as 'p'
FROM Products p
-- Join with the subquery result aliased as 's'
JOIN (
-- Select supplier_id and supplier_name from Suppliers table where the city is 'New York'
SELECT supplier_id,
supplier_name
FROM Suppliers
WHERE city = 'New York'
) AS s
-- On matching supplier_id between Products and the subquery result
ON p.supplier_id = s.supplier_id;
SQL Upskilling for Beginners
More Advanced SQL Alias Ideas
The following examples show some advanced techniques involving SQL alias.
SQL alias with other functions
The use of an SQL alias is important when using complex queries that require explanatory output. Providing an SQL alias to the transformed data involves functions, expressions, or calculations.
Using functions
The query below uses a SQL alias to change the name of a column. This column also changes because all the values are converted to uppercase using the UPPER()
function. This other function doesn’t affect the use of the AS
keyword, although we might also want to show the change by converting the name to supplier_uppercase
.
-- Select product names from the Products table
SELECT product_name,
-- Convert supplier to uppercase, alias as 'supplier_uppercase'
UPPER(supplier_name) AS supplier_uppercase
-- From the Products table aliased as 'p'
FROM Products p
-- Join with the Suppliers table aliased as 's' on matching supplier_id
JOIN Suppliers s ON p.supplier_id = s.supplier_id;
Example of SQL output using functions. Image by Author.
Using expressions
The query below shows the SQL alias column price_with_tax
, which is derived from a calculation in the SELECT
statement.
-- Select product names from the Products table
SELECT product_name,
-- Select product prices from the Products table
price,
-- Calculate the price with a 10% tax and alias as 'price_with_tax'
price * 1.1 AS price_with_tax
-- From the Products table
FROM Products;
Example of SQL output using expressions. Image by Author.
Debugging and maintaining queries
If you have queries that use more than one SQL alias, you should consider the following practices for debugging.
- Use consistent and meaningful naming of each SQL alias.
- Test smaller subqueries before combining them in one query.
- Add appropriate comments on the SQL queries to explain each name.
Conclusion and Further Learning
Understanding how to use a SQL alias is an important part of writing queries. In this article, you learned how to use aliases to rename columns and tables when temporarily writing complex queries. I encourage you to continue practicing the different scenarios where you can apply SQL aliases to improve your skills.
If you want to learn advanced SQL skills, I recommend taking DataCamp’s Intermediate SQL course. You should also try our Associate Data Analyst in SQL career track to help you become a proficient data analyst. Finally, I highly recommend obtaining DataCamp’s SQL Associate Certification to demonstrate your application of SQL skills and stand out among other data scientists and data analysts.
Earn a Top SQL Certification
Frequently Asked Questions
What is an SQL alias?
SQL alias is a temporary column or table name in a query. It is used to make the column or table name more readable or convenient.
What is the AS keyword?
The AS
keyword is used to reference the column or table to rename. The AS
keyword is optional and can be omitted in the query.
Which SQL databases support aliases?
All the major SQL databases support SQL aliases, including SQL Server, MySQL, and PostgreSQL.
Can you use numbers in a SQL alias?
Yes, you can use numbers in an SQL alias, but they cannot start with a number without using quotes or brackets.
Why is it impossible to dynamically set a column's alias?
Dynamic column aliases aren't possible because SQL requires column names to be fixed during compilation, while dynamic aliases would only be determined during execution. This conflicts with the pre-compilation process.