beginner
Renaming Output with Aliases
Use AS to give columns and tables friendlier, more readable names in your query results.
4 min read
Explanation
Sometimes a column's real name isn't the clearest label for your results, or a query gets hard to read once you're referencing several tables. An alias lets you give a column, or a table, a temporary name that only applies within that query.
Take the students table:
| id | name | grade_level | enrollment_year |
|---|---|---|---|
| 1 | Maya Torres | 9 | 2023 |
| 2 | Jonah Lee | 10 | 2022 |
Running SELECT name FROM students gives you a column literally labeled
name. If your result is going into a report alongside other kinds of
names, SELECT name AS student_name FROM students makes the output much
clearer without changing anything in the actual table.
Aliases are just labels for the result
An alias exists only for the duration of the query. It doesn't rename anything in the database — it just changes what you see in the output.
Syntax
Aliasing a column:
SELECT column_name AS friendlier_name
FROM table_name;Aliasing a table (useful once you start writing longer queries):
SELECT s.name
FROM students AS s;AS is optional — column_name friendlier_name works too — but including
it makes the intent obvious at a glance.
Interactive Example
Loading database engine...
Loading database engine...
Common Mistakes
- Trying to use a column alias inside the same query's WHERE clause.
Most databases evaluate
WHEREbefore the alias is assigned, soWHERE grade = 9(using the real column name) is what you need, notWHERE grade_alias = 9. - Forgetting quotes around an alias with spaces. If you want an alias
containing a space, like
"Student Name", it needs to be quoted; otherwise stick to a single word with no spaces. - Confusing a table alias with renaming the table.
FROM students AS sonly creates a shorthand name for use within that query — the table is still calledstudentseverywhere else.
Best Practices
- Use column aliases to make ambiguous or cryptic column names readable in your results, especially for scores, counts, or calculated values.
- Use short table aliases (like
sforstudents) once your queries start involving more than one table — it keeps things concise without hiding meaning. - Keep alias names short but descriptive, and prefer
ASexplicitly even though it's optional, since it reads clearly to others.
Practice Question
Using the first playground above, write a query against the students
table that returns the enrollment_year column aliased as year_joined,
alongside the student's name.
Summary
Aliases, created with AS, let you rename columns and tables just for the
scope of a query, without touching the underlying schema. They make results
more readable and become especially useful once queries reference multiple
tables.