Skip to content

MySQL - Alias

In SQL, an alias is a temporary, alternative name given to a table or a column in a query. Aliases are used to make column names more readable and to simplify queries, especially when working with JOIN operations or complex expressions.

Aliases are assigned using the AS keyword. While the AS keyword is technically optional in MySQL, using it is a strong best practice as it makes the query’s intent much clearer.

Column aliases are frequently used to provide more descriptive names for the columns in your result set or to name a column derived from an expression or aggregate function.

SELECT column_name AS alias_name
FROM table_name;
-- Or with an expression
SELECT expression AS alias_name
FROM table_name;

Let’s use the same customers table from previous examples.

SELECT * FROM customers LIMIT 3;
idnameageaddresssalary
1Ramesh32Ahmedabad2000.00
2Khilan25Delhi1500.00
3Kaushik23Kota2000.00

In this query, we rename the id and name columns in the output.

SELECT id AS customer_id, name AS customer_name
FROM customers;

The result set will have headers customer_id and customer_name.

A very common use case is to name the result of an aggregate function like COUNT(), SUM(), or AVG().

SELECT COUNT(id) AS total_customers, AVG(salary) AS average_salary
FROM customers;

This produces a clean, readable result:

total_customersaverage_salary
75000.00

You can create a new virtual column from existing data and give it an alias.

SELECT id, CONCAT(name, ' from ', address) AS location_info
FROM customers;
idlocation_info
1Ramesh from Ahmedabad
2Khilan from Delhi
…and so on…

Table aliases are essential for writing concise and readable JOIN queries. They are mandatory when performing a self-join.

The modern, explicit JOIN syntax is strongly preferred over the old comma-based syntax because it clearly separates join conditions from filtering conditions (WHERE clause).

SELECT t1.column, t2.column
FROM table1 AS t1
JOIN table2 AS t2 ON t1.common_column = t2.common_column;

Let’s create an orders table and join it with customers to find out who ordered what.

CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
amount DECIMAL(10, 2)
);
INSERT INTO orders VALUES
(101, 3, '2023-10-08', 3000.00),
(102, 2, '2023-11-20', 1560.00),
(103, 3, '2023-10-08', 1500.00),
(104, 4, '2023-05-20', 2060.00);

Now, let’s join them using short, clear aliases (c for customers, o for orders).

SELECT c.name, c.address, o.order_date, o.amount
FROM customers AS c
JOIN orders AS o ON c.id = o.customer_id
ORDER BY c.name;

This would produce the following result:

nameaddressorder_dateamount
ChaitaliMumbai2023-05-202060.00
KaushikKota2023-10-083000.00
KaushikKota2023-10-081500.00
KhilanDelhi2023-11-201560.00

A self-join is when you join a table to itself. In this case, table aliases are not just helpful—they are required to distinguish between the two instances of the same table.

Example: Finding Employees and Their Managers

Section titled “Example: Finding Employees and Their Managers”

A classic example is an employees table where a manager_id column refers back to another employee’s id in the same table. We can use a self-join to list each employee next to their manager’s name.

CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
manager_id INT
);
INSERT INTO employees VALUES
(1, 'Alice', NULL), -- The CEO has no manager
(2, 'Bob', 1),
(3, 'Charlie', 1),
(4, 'David', 2);

Here, we join employees to itself, aliasing one instance as e (for employee) and the other as m (for manager). A LEFT JOIN is used to include the CEO who has no manager.

SELECT
e.name AS employee_name,
m.name AS manager_name
FROM
employees AS e
LEFT JOIN employees AS m ON e.manager_id = m.id;

Output of the above query is as follows:

employee_namemanager_name
Alice
BobAlice
CharlieAlice
DavidBob