Skip to main content
Log inGet a demo
← SQL Dictionary
Basic statements and clauses

SQL Aliases

What is a SQL Alias?

An SQL alias is a user-defined name or temporary label that is assigned to a table or column within an SQL query. Aliases are used to make SQL queries more readable, concise, and to provide alternate names for tables or columns. They do not change the underlying database schema but simplify query writing and enhance result clarity.

When to Use SQL Aliases

You would use SQL aliases when:

  1. Column Renaming: You want to rename columns in the query result for better readability or to avoid naming conflicts.
  2. Table Renaming: You need to reference a table by a shorter, more intuitive name in complex queries.
  3. Aggregations: When performing aggregate functions (e.g., COUNT, SUM, AVG), you can use aliases to provide meaningful names for the computed values.
  4. Self-Joins: In self-joins, you use aliases to distinguish between different instances of the same table.
  5. Subqueries: In subqueries or derived tables, aliases help reference the subquery result.

Syntax of SQL 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;

Parameter Values

  • column_name: The name of the column you want to alias.
  • alias_name: The name you want to assign as an alias for the column or table.
  • table_name: The name of the table you are querying.

Example Query

Suppose we have a table named employees with columns employee_id, first_name, and last_name. We want to create an alias for the first_name column.

SELECT first_name AS "First Name"
FROM employees;

Example Table Response

The result of the query would look like this:

First Name
-------------
John
Alice
Bob
Eve

Use Cases for SQL Aliases

  1. Improving Readability: Aliases make query results more human-readable by providing descriptive column names.
  2. Aggregations: Aliases are used when performing aggregations like summing or averaging, to provide clear labels for computed values.
  3. Self-Joins: In self-joins, aliases help differentiate between tables of the same structure.
  4. Subqueries: Aliases are essential when referencing subquery results within the main query.

SQL Dialects Supporting Aliases

SQL aliases are supported in most major SQL dialects, including but not limited to:

  • MySQL
  • PostgreSQL
  • SQLite
  • SQL Server
  • Oracle
  • IBM Db2
  • Teradata
  • and more.

Always refer to the specific documentation of the SQL database management system you are using for any unique syntax or limitations related to aliases.


Keep reading

SQL WITH

Read article

SQL Comments

Read article

SQL Equal To

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.