About Lesson
MySQL Aliases
An alias in MySQL is a temporary name assigned to a table or column in a query. It is used to make column names more readable or to shorten table names.
Alias for Columns:
You can assign an alias to a column in the SELECT statement.
SELECT first_name AS 'First Name', last_name AS 'Last Name' FROM employees;
This query renames the columns ‘first_name’ and ‘last_name’ to ‘First Name’ and ‘Last Name’, respectively.
Alias for Tables:
You can assign an alias to a table to simplify the query.
SELECT e.first_name, e.last_name, d.department_name
FROM employees AS e
JOIN departments AS d ON e.department_id = d.department_id;
This query uses ‘e’ and ‘d’ as aliases for the ’employees’ and ‘departments’ tables, respectively.
Aliases make queries more readable and allow for easier referencing of columns and tables.