LearnContact
Lesson 2714 min read

Aliases

Learn how to give columns and tables temporary, friendlier names using the AS keyword.

Introduction

Sometimes a column's real name is not the name you want to show to a user, or a table's real name is too long to type over and over inside a query. MySQL lets you give both columns and tables a temporary, alternate name for the duration of a single query. This is called an alias.

Aliases do not change anything in the database itself — they only affect how the result looks, or how you refer to something inside that one query.

What You Will Learn
  • What an alias is and why it is useful.
  • How to alias a column with AS to give the result set a friendlier header.
  • That AS is optional for column aliases.
  • How to alias a table with a short name.
  • A worked example combining both.

What is an Alias?

An alias is a temporary name you assign to a column or a table, using the AS keyword. It exists only for the query it is used in — it is never saved anywhere in the database.

Aliases are useful for two main reasons: making result columns easier to read, and making queries with multiple tables shorter and easier to write.

Use CaseExample
Rename a column in the outputSELECT name AS student_name FROM students;
Give a table a short nicknameSELECT s.name FROM students AS s;

Column Aliases

Consider a "students" table. The column is named "name" in the database, but you might want the result to display a clearer header, like "student_name."

SELECT name AS student_name, marks AS total_marks
FROM students;
Result
+---------------+-------------+
| student_name  | total_marks |
+---------------+-------------+
| Alex          | 78          |
| Sam           | 65          |
| Priya         | 91          |
+---------------+-------------+

Notice that only the column headers changed. The underlying "students" table still has columns literally named "name" and "marks" — the alias only affects this one result set.

When to Use Column Aliases

Column aliases are especially useful when a column contains a calculation, like SUM(marks) AS total_marks, since the raw expression makes for an ugly, hard-to-read header otherwise.

AS is Optional

For column aliases, the AS keyword is actually optional. MySQL allows you to simply put a space between the column and the alias name, and it will be understood the same way.

-- These two queries produce identical results
SELECT name AS student_name FROM students;
SELECT name student_name FROM students;
Style Recommendation

Even though AS is optional, most developers include it anyway because it makes the query easier to read at a glance — especially for anyone new to the query.

Table Aliases

Table aliases work the same way, but they give a table a short nickname instead of a column. This becomes far more valuable once you start combining multiple tables together using joins (covered in a later lesson) because typing a full table name repeatedly for every column reference gets tedious and hard to read.

SELECT s.name, s.marks
FROM students AS s
WHERE s.marks > 70;
Result
+-------+-------+
| name  | marks |
+-------+-------+
| Alex  | 78    |
| Priya | 91    |
+-------+-------+

Here, "students" is aliased as "s." Every reference to a column from that table can now use the short prefix "s." instead of "students." — a small change now, but a huge readability win once a query involves two or three tables at once.

Looking Ahead

Table aliases become essential once you learn JOINs later in this course, since a single query often needs to reference columns from two or more tables and must be able to tell them apart clearly.

Worked Example

Suppose you want a clean, presentation-ready report of student names and their scores, using a short table alias as good practice even though this query only involves one table.

aliases_example.sql
SELECT s.name AS student_name, s.marks AS score
FROM students AS s
ORDER BY score DESC;
Result
+---------------+-------+
| student_name  | score |
+---------------+-------+
| Priya         | 91    |
| Alex          | 78    |
| Sam           | 65    |
+---------------+-------+

Notice that the ORDER BY clause can refer directly to the column alias "score" — MySQL resolves aliases in ORDER BY, which makes queries like this one both shorter and easier to read.

Common Mistakes

Avoid These Mistakes
  • Trying to use a column alias inside the WHERE clause of the same query — WHERE is evaluated before aliases exist, so this causes an error.
  • Forgetting quotes around an alias that contains a space, like AS 'student name' — without quotes, MySQL will error on the space.
  • Assuming an alias changes the actual column or table name permanently — it only applies to that one query.
  • Overusing cryptic single-letter table aliases in large queries, making the query hard for others to follow.

Best Practices

  • Use AS for column aliases even though it is optional, for clarity.
  • Pick short but meaningful table aliases (like "s" for students, "o" for orders), not single unrelated letters.
  • Alias any column built from a calculation or function so the result has a readable header.
  • Keep alias names consistent across related queries in the same project.

Frequently Asked Questions

Does an alias change the table or column name in the database?

No. An alias only exists for the duration of the query it is written in. The real table and column names are never affected.

Can I use a column alias inside a WHERE clause?

No. MySQL evaluates WHERE before column aliases are assigned, so referencing an alias there will cause an error. You can, however, use aliases in ORDER BY and GROUP BY.

Is AS required?

It is required for table aliases in some contexts, but for column aliases it is optional. Most developers include it anyway for readability.

Why bother aliasing a table if my query only uses one table?

It is not strictly necessary for a single table, but it builds a habit that pays off enormously once your queries start joining multiple tables together.

Key Takeaways

  • An alias is a temporary name for a column or table that only applies within a single query.
  • Column aliases use AS to give result columns friendlier headers.
  • AS is optional for column aliases, though including it improves readability.
  • Table aliases give a table a short nickname, which becomes essential once you start writing joins.
  • Aliases never change anything in the actual database structure.

Summary

Aliases let you rename columns and tables for the lifetime of a single query, using the AS keyword. Column aliases make result sets easier to read, while table aliases make complex, multi-table queries far shorter and clearer.

In this lesson, you learned how to alias both columns and tables, and saw that AS is optional for columns. Next, you will learn how to modify existing data using the UPDATE statement.

Lesson 27 Completed
  • You can alias a column for a friendlier result header.
  • You know that AS is optional for column aliases.
  • You can alias a table with a short nickname.
  • You are ready to learn how to update existing rows.
Next Lesson →

UPDATE Statement