Aliasing

Aliasing is the process of giving a table or a table column a temporary name. Aliases are commonly used to either make the query easier to write, or more readable. An example of using table aliases to make a query shorter would be the following.

SELECT
	orders.description,
	orders.value,
	customers.email
FROM
	orders
	INNER JOIN customers ON order.customer_id = customers.id;

By using table aliases, the query length can be greatly reduced.

SELECT
	o.description,
	o.value,
	c.email
FROM
	orders AS o
	INNER JOIN customers AS c ON o.customer_id = c.id;

Note that aliases only last for the duration of a single query. If we were to run the previous query, and subsequently run the following query, it would fail.

SELECT
	o.description,
	o.value,
	c.email
FROM
	o
	INNER JOIN c ON o.customer_id = c.id;

In addition to table aliases, we can give fields aliases as well. For example, we could reduce customer_id to just c_id.

SELECT
	orders.customer_id AS c_id
FROM
	orders
	INNER JOIN customers ON order.c_id = customers.id;

Alternatively, we could change customer_id to Customer ID, however, whenever we want an alias to contain spaces, we need to use either double quotes or square brackets.

SELECT
	orders.customer_id AS "Customer ID"
FROM
	orders
	INNER JOIN customers ON order. "Customer ID" = customers.id;