Sqlite Aliases

SQLite: ALIASES

This SQLite post explains how to use SQLite ALIASES (temporary names for columns or tables) with syntax and examples.

Description

SQLite ALIASES can be used to create a temporary name for columns or tables.

  • COLUMN ALIASES are used to make column headings in your result set easier to read.
  • TABLE ALIASES are used to shorten your SQL to make it easier to read or when you are performing a self join (ie: listing the same table more than once in the FROM clause).

Syntax

The syntax to ALIAS A COLUMN in SQLite is:

column_name [ AS ] alias_name

OR

The syntax to ALIAS A TABLE in SQLite is:

table_name [ AS ] alias_name

Parameters or Arguments

column_name

The original name of the column that you wish to alias.

table_name

The original name of the table that you wish to alias.

AS

Optional. Most programmers will specify the AS keyword when aliasing a column name, but not when aliasing a table name. Whether you specify the AS keyword or not has no impact on the alias in SQLite. It is a personal choice in SQLite, unlike other databases. (Our examples will use AS when aliasing a column name but omit AS when aliasing a table name.)

alias_name

The temporary name to assign to the table or column.

Note

  • If the alias_name contains spaces, you must enclose the alias_name in quotes.
  • It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces when you are aliasing a table name.
  • The alias_name is only valid within the scope of the SQL statement.

Example - ALIAS a column

Generally, aliases are used to make the column headings in your result set easier to read. For example, when using the MAX function, you might alias the result of the MAX function in SQLite.

For example:

SELECT department_id, MAX(employee_id) AS largest
FROM employees
GROUP BY department_id;

In this example, we've aliased the MAX(employee) field as largest. As a result, largest will display as the heading for the second column when the result set is returned. Because our alias_name did not include any spaces, we are not required to enclose the alias_name in quotes.

However, it would have been perfectly acceptable to write this example using quotes as follows:

SELECT department_id, MAX(employee_id) AS "largest"
FROM employees
GROUP BY department_id;

Next, let's look at an example where we are required to enclose the alias_name in quotes.

For example:

SELECT department_id, MAX(employee_id) AS "largest ID"
FROM employees
GROUP BY department_id;

In this example, we've aliased the MAX(employee_id) field as "largest ID". Since there are spaces in this alias_name, "largest ID" must be enclosed in quotes.

Example - ALIAS a Table

When you create an alias on a table, it is either because you plan to list the same table name more than once in the FROM clause (ie: self join), or you want to shorten the table name to make the SQL statement shorter and easier to read.

Let's look at an example of how to alias a table name in SQLite.

For example:

SELECT e.employee_id, e.last_name, departments.department_name FROM employees e INNER JOIN departments ON e.department_id = departments.department_id
ORDER BY e.last_name DESC, departments.department_name ASC;

In this example, we've created an alias for the employees table called e. Now within this SQL statement, we can refer to the employees table as e.

When creating table aliases, it is not necessary to create aliases for all of the tables listed in the FROM clause. You can choose to create aliases on any or all of the tables.

For example, we could modify our example above and create an alias for the departments table as well.

SELECT e.employee_id, e.last_name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id
ORDER BY e.last_name DESC, d.department_name ASC;

Now we have an alias for departments table called d as well as the alias for the employees table called e.