An alias is a temporary name that exists only for one query. It makes results readable and queries shorter.
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
category VARCHAR(50) NOT NULL,
price DECIMAL(8,2) NOT NULL,
stock INT NOT NULL DEFAULT 0,
added_on DATE NOT NULL
);
INSERT INTO products (name, category, price, stock, added_on) VALUES
('Notebook', 'stationery', 80.00, 120, '2026-01-10'),
('Gel Pen', 'stationery', 15.50, 300, '2026-01-12'),
('Backpack', 'bags', 999.00, 25, '2026-02-01'),
('Marker Set', 'stationery', 145.00, 0, '2026-02-15'),
('Desk Lamp', 'home', 650.00, 40, '2026-03-03'),
('Water Bottle', 'home', 220.00, 75, '2026-03-20'),
('Laptop Sleeve', 'bags', 450.00, 18, '2026-04-02'),
('Sticky Notes', 'stationery', 35.00, 210, '2026-04-18');
Column aliases
SELECT name AS product, price * stock AS stock_value
FROM products
WHERE stock > 0
ORDER BY stock_value DESC
LIMIT 3;
+--------------+-------------+
| product | stock_value |
+--------------+-------------+
| Desk Lamp | 26000.00 |
| Backpack | 24975.00 |
| Water Bottle | 16500.00 |
+--------------+-------------+
3 rows in set
Without AS stock_value, the column header would be the whole expression price * stock. The alias is also what your application code reads ($row['stock_value']).
AS is optional (price * stock stock_value works), but always write it. Without it, a forgotten comma turns into an accidental alias. This query is missing a comma:
SELECT name price FROM products LIMIT 2;
+----------+
| price |
+----------+
| Notebook |
| Gel Pen |
+----------+
2 rows in set
MySQL did not complain. It read price as an alias for name, so the result has a column called price holding the product names. AS makes such a mistake easy to spot.
Aliases with spaces
Wrap an alias that contains spaces or special characters in backticks:
SELECT name AS `Product Name`, price AS `Price (INR)` FROM products LIMIT 2;
+--------------+-------------+
| Product Name | Price (INR) |
+--------------+-------------+
| Notebook | 80.00 |
| Gel Pen | 15.50 |
+--------------+-------------+
2 rows in set
Aliases with spaces are for reports. In application code use snake_case names.
Table aliases
A table alias shortens the table name, which matters in joins:
SELECT p.name, p.price
FROM products AS p
WHERE p.category = 'home';
+--------------+--------+
| name | price |
+--------------+--------+
| Desk Lamp | 650.00 |
| Water Bottle | 220.00 |
+--------------+--------+
2 rows in set
Here p stands for products. It becomes necessary in a self-join, where the same table appears twice (Self Joins), and when you join two tables that have columns with the same name.
Where you can use an alias
| Clause | Column alias allowed? | Why |
|---|---|---|
ORDER BY | Yes | It runs after SELECT |
GROUP BY | Yes in MySQL (not standard) | MySQL extension |
HAVING | Yes in MySQL (not standard) | MySQL extension |
WHERE | No | It runs before SELECT |
Another select expression in the same SELECT | No | Not available yet |
SELECT price * stock AS stock_value FROM products WHERE stock_value > 5000;
ERROR 1054 (42S22): Unknown column 'stock_value' in 'where clause'
WHERE cannot see stock_value. Repeat the expression, or wrap the query in a derived table:
SELECT name, stock_value
FROM (SELECT name, price * stock AS stock_value FROM products) AS t
WHERE stock_value > 5000
ORDER BY stock_value DESC;
+---------------+-------------+
| name | stock_value |
+---------------+-------------+
| Desk Lamp | 26000.00 |
| Backpack | 24975.00 |
| Water Bottle | 16500.00 |
| Notebook | 9600.00 |
| Laptop Sleeve | 8100.00 |
| Sticky Notes | 7350.00 |
+---------------+-------------+
6 rows in set
Subquery in FROM needs an alias
FROM (SELECT ...) without AS t gives error 1248, “Every derived table must have its own alias”.