HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL Aliases (AS)

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.


Column Aliases

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_namehometownyears_old
Rahim UddinDhaka22
Sara BegumChittagong20
Karim AliSylhet23
Note: The 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 for Expressions

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;

Aliases with Spaces

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;
Best Practice: Use underscores instead of spaces in aliases — full_name instead of `Full Name`. Underscore aliases do not need quotes and work reliably across all MySQL tools and drivers.

Using Aliases in ORDER BY

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;
Note: You cannot use a column alias in a 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.

Table Aliases

You can also alias a table name within a query. Table aliases are most useful when:

  • Writing JOIN queries where you need to prefix columns with the table name.
  • The table name is long and you want a short abbreviation throughout the query.
-- 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;

Key Points to Remember

  • An alias is a temporary name — it does not change the actual column or table name in the database.
  • Use AS for column aliases to rename output columns and make expressions readable.
  • Aliases with spaces must be wrapped in backticks or quotes — prefer underscores to avoid this.
  • Column aliases can be used in ORDER BY but not in WHERE or HAVING.
  • Table aliases shorten query syntax and are essential for clean JOIN queries.
  • The AS keyword is optional, but always including it makes queries easier to understand.
What's next? You have completed the Filtering & Sorting section. The next section covers Aggregate Functions — starting with MIN & MAX, which let you find the smallest and largest values in a column.