Aliases in Joins
As SQL queries grow more complex, using aliases helps improve readability and structure.
Aliases allow you to assign temporary, clear names to tables and columns, making multi-join queries easier to follow.
Table Aliases
Use the AS
keyword (or omit it) to give a meaningful alias to a table:
Table alias with AS
SELECT students.name, enrollments.class_id
FROM students AS students
JOIN enrollments AS enrollments
ON students.student_id = enrollments.student_id;
You can also omit AS
:
Table alias without AS
FROM students students
JOIN enrollments enrollments ...
This avoids repeating long table names and is useful when joining multiple tables.
Column Aliases
Use AS
to rename output columns for clarity:
Column alias
SELECT
students.name AS student_name,
enrollments.class_id AS course
FROM students students
JOIN enrollments enrollments
ON students.student_id = enrollments.student_id;
Output
student_name | course |
---|---|
Alex | A1 |
Sara | A2 |
Column aliases help make query results easier to read and are especially helpful for dashboards or reports.
When to Use Aliases
You should use aliases when:
- Joining multiple tables with similar column names
- Performing self joins (you must alias the same table twice)
- Writing readable queries for analysis, reporting, or BI dashboards
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.