Aliases in SQL
To make column names or table names more readable, we can use aliases:
SQL
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;
In this query, e and d are aliases for the employees and departments tables, respectively.
In SQL, aliases are used to give a table or a column a temporary name. This can make your SQL queries more readable and can also be useful when you need to perform operations on the same table or column more than once within a query. Aliases are created using the AS keyword, but it is optional and often omitted.
Column Alias A column alias allows you to rename a column in the result set. Example:
SQL
FROM employees;
In this example:
- first_name is renamed to fname in the result set.
- last_name is renamed to lname in the result set.
- email remains unchanged.
Table Alias A table alias allows you to give a table a temporary name, which is useful for shortening table names or for self-joins. Example:
SQL
FROM employees AS e;
In this example:
- The employees table is given the alias e.
- Columns from the employees table are referenced using the alias e.