Skip to content

MySQL Tutorial Reading Data with SELECT

Aliases: AS for Columns and Tables

Lesson 80 of 308 2 min read By Deepak Paswan Updated Oct 5, 2026

An alias is a temporary name that exists only for one query. It makes results readable and queries shorter.

query.sql
SQL
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

query.sql
SQL
SELECT name AS product, price * stock AS stock_value
FROM products
WHERE stock > 0
ORDER BY stock_value DESC
LIMIT 3;
Output
OUTPUT
+--------------+-------------+
| 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:

query.sql
SQL
SELECT name price FROM products LIMIT 2;
Output
OUTPUT
+----------+
| 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:

query.sql
SQL
SELECT name AS `Product Name`, price AS `Price (INR)` FROM products LIMIT 2;
Output
OUTPUT
+--------------+-------------+
| 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:

query.sql
SQL
SELECT p.name, p.price
FROM products AS p
WHERE p.category = 'home';
Output
OUTPUT
+--------------+--------+
| 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

ClauseColumn alias allowed?Why
ORDER BYYesIt runs after SELECT
GROUP BYYes in MySQL (not standard)MySQL extension
HAVINGYes in MySQL (not standard)MySQL extension
WHERENoIt runs before SELECT
Another select expression in the same SELECTNoNot available yet
query.sql
SQL
SELECT price * stock AS stock_value FROM products WHERE stock_value > 5000;
Output
OUTPUT
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:

query.sql
SQL
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;
Output
OUTPUT
+---------------+-------------+
| 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
warning
Subquery in FROM needs an alias
In MySQL every derived table must have an alias. FROM (SELECT ...) without AS t gives error 1248, “Every derived table must have its own alias”.