Explain the use of 'Aliases' in SQL queries.
Instruction: Discuss how aliases are used in SQL and why they might be necessary.
Updated
Official answer
Aliases name table references and output expressions within a query. They improve clarity and distinguish multiple uses of the same table; they do not inherently accelerate execution or rename stored schema objects.
For PostgreSQL employees(id integer, name text, manager_id integer):
SELECT e.name AS employee_name, m.name AS manager_name
FROM employees AS e
LEFT JOIN employees AS m ON m.id = e.manager_id
ORDER BY e.id;
The aliases distinguish employee and manager roles. The left join keeps an employee without a matching manager. If manager IDs are not unique, matches can multiply; aliases do not repair the data model.
An output alias is not available everywhere in the same query level. PostgreSQL allows many output-alias uses in ORDER BY but not as a substitute for the expression in WHERE. Use a subquery when a later filter needs a named derived value. Follow the database's name-resolution rules and prefer meaningful names over ambiguous abbreviations.