What is an alias in SQL?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid MySQL / SQL basics — a prerequisite for any developer role.

Answer

An alias is a temporary name assigned to a table or column for the duration of a query, improving readability and allowing shorter references. Column aliases: SELECT first_name AS name, price * quantity AS total_cost FROM orders; — the AS keyword is optional (first_name name also works, but AS improves clarity). Column aliases are available in ORDER BY and HAVING but NOT in WHERE (because WHERE is evaluated before SELECT). Table aliases: essential when joining tables to avoid ambiguous column names and shorten long table names: SELECT u.name, o.total FROM users AS u JOIN orders AS o ON u.id = o.user_id;. Table aliases are used throughout the query after they are defined. Required aliases: subqueries used in FROM must always have an alias: SELECT * FROM (SELECT id, name FROM users) AS active_users;. In self-joins (joining a table to itself), aliases are mandatory to distinguish the two instances: FROM employees AS manager JOIN employees AS report ON manager.id = report.manager_id.

Pro Tip

This topic has MySQL / SQL-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.