An alias is a temporary name you assign to a column or table within a query using the AS keyword. The alias only exists for the duration of that query — the actual column or table name in the database is never changed. Aliases make results easier to read, expressions more meaningful, and queries involving multiple tables much cleaner to write.
A column alias renames a column in the query's output. This is useful when column names are technical, abbreviated, or when you are displaying the result of an expression:
SELECT name AS student_name, city AS hometown, age AS years_old
FROM students;
| student_name | hometown | years_old |
|---|---|---|
| Rahim Uddin | Dhaka | 22 |
| Sara Begum | Chittagong | 20 |
| Karim Ali | Sylhet | 23 |
AS keyword is optional — you can write the alias directly after the column name. However, always including AS makes the query clearer and easier to read: name AS student_name is better than name student_name.
Aliases are especially valuable when the SELECT list contains calculated expressions — without an alias, the output column would show the raw expression as its header:
-- Without alias: column header is "age + 1"
SELECT name, age + 1 FROM students;
-- With alias: column header is "next_year_age"
SELECT name, age + 1 AS next_year_age FROM students;
-- Combining columns into one readable field
SELECT
CONCAT(first_name, ' ', last_name) AS full_name,
YEAR(CURDATE()) - YEAR(birth_date) AS age
FROM employees;
If an alias contains spaces or special characters, wrap it in backticks or double quotes:
SELECT name AS `Full Name`, city AS `Home City`
FROM students;
full_name instead of `Full Name`. Underscore aliases do not need quotes and work reliably across all MySQL tools and drivers.
Column aliases can be referenced in the ORDER BY clause because MySQL evaluates ORDER BY after processing the SELECT list:
SELECT name, age + 1 AS next_year_age
FROM students
ORDER BY next_year_age DESC;
WHERE clause — MySQL evaluates WHERE before processing the SELECT list, so the alias does not exist yet at that point. Use the full expression in WHERE instead.
You can also alias a table name within a query. Table aliases are most useful when:
JOIN queries where you need to prefix columns with the table name.-- Without table alias: verbose
SELECT students.name, students.city
FROM students
WHERE students.age > 21;
-- With table alias: concise
SELECT s.name, s.city
FROM students AS s
WHERE s.age > 21;
In a JOIN, table aliases make the query far more readable:
SELECT s.name, c.course_name
FROM students AS s
JOIN enrollments AS e ON s.id = e.student_id
JOIN courses AS c ON e.course_id = c.id;
AS for column aliases to rename output columns and make expressions readable.ORDER BY but not in WHERE or HAVING.JOIN queries.AS keyword is optional, but always including it makes queries easier to understand.