Hook: Aliases are like name tags at a conference: they do not change who the person is, but they make the conversation much easier.
Question: What are aliases in SQL?
Answer: An alias is a temporary name you give to a column or a table inside one query. You use it to make output easier to read, shorten long table names, or name a computed value like salary * 12. It does not rename the real column in the database, and it only exists for that statement.
Interview-Ready Answer: I use aliases to give a column or table a temporary name inside a query. For example, I might write salary * 12 AS annual_salary or employees AS e to make the SQL shorter and clearer. The key detail is that aliases only exist for that query result; they do not change the table schema, and in most databases I cannot use a SELECT alias in the WHERE clause because WHERE is evaluated earlier.
Detailed Explanation: Aliases are temporary labels, not real data changes. Think of them as query-time nicknames: the database keeps the original table and column names, but it shows a friendlier name in the result set or lets you reference a table with a shorter label inside the query.
SELECT statement or one subquery level.FROM, JOIN, and ON. That is why employees AS e lets you write e.salary later.WHERE runs before the SELECT list is produced. So a column alias like annual_salary usually does not exist yet when filtering happens.SELECT expressions and attaches column aliases to the output metadata. Metadata means the descriptive info about the result shape, such as column names.ORDER BY runs after SELECT, so it can usually see the final output names. That is why ORDER BY annual_salary works in most databases.| Type | What it names | Example | Typical use |
|---|---|---|---|
| Column alias | One output column | salary * 12 AS annual_salary | Readable reports |
| Table alias | A table or subquery | employees AS e | Short joins |
The big mental model is this: a column alias changes the label on the result, while a table alias changes the short name you use to point at a source inside the query. A table alias is especially useful for self-joins, where the same table appears twice and you need two different names for it.
price * quantity, and you want a clean report name like line_total.customer_order_history, or when joining multiple tables would otherwise repeat long prefixes everywhere.WHERE: usually cannot see a column alias, because filtering happens before the select list exists.ORDER BY: usually can see a column alias, because sorting happens after the select list is created.GROUP BY and HAVING: support varies by dialect, so the safest portable habit is to repeat the expression or use a subquery/CTE.SELECT list usually cannot reuse a fresh alias from an earlier expression in that same list.Aliases are almost free. They do not add a table scan, index lookup, or join. Name resolution is just metadata work, so the overhead is tiny compared with real work like scanning rows, joining tables, or sorting results. If a query sorts 1 million rows, the sort still costs roughly O(n log n); the alias does not change that. In other words, aliases improve readability, not speed.
Two important edge cases matter in interviews. First, a derived table in the FROM clause usually must have an alias, because the engine needs a name for that temporary result. Second, do not assume every database treats alias visibility the same way. Some dialects are permissive, but portable SQL is stricter, and interviewers usually want the standard mental model.
Memory picture: aliases are sticky notes on the result, not new names on the filing cabinet. The data in the cabinet stays the same; only the label on the paper you are reading changes.
Real-World Example: In a checkout service, a nightly revenue job calculates net_revenue from order totals and refunds. The engineer writes a filter like WHERE net_revenue > 0 in the same query layer, expecting the alias to behave like a real column. In production, the job fails with an error such as column net_revenue does not exist, the dashboard stops refreshing, and finance sees stale numbers during the morning stand-up.
What happened? The alias only existed after the SELECT list was built, but WHERE ran earlier. The fix is to compute the value in a subquery or CTE, then filter in the outer query. Once the team did that, the report became stable and the logs stopped filling with repeated query errors.
That is why aliases matter beyond style: a small misunderstanding can break a production report, hide revenue drops, or make a support team chase a bug that is really just query timing.
-- Alias demo: column aliases, table aliases, and the common WHERE gotcha.
-- This script is written in standard-ish SQL and is safe to run as-is.
DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS departments;
CREATE TABLE departments (
department_id INTEGER PRIMARY KEY,
department_name VARCHAR(50) NOT NULL
);
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department_id INTEGER NOT NULL,
salary INTEGER NOT NULL,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
INSERT INTO departments (department_id, department_name) VALUES
(1, 'Engineering'),
(2, 'Sales'),
(3, 'Finance');
INSERT INTO employees (employee_id, first_name, last_name, department_id, salary) VALUES
(101, 'Ava', 'Patel', 1, 90000),
(102, 'Noah', 'Kim', 1, 120000),
(103, 'Mia', 'Lopez', 2, 70000),
(104, 'Zoe', 'Chen', 3, 50000);
-- Column alias: the output header becomes easier to read.
SELECT
employee_id,
first_name,
salary * 12 AS annual_salary
FROM employees
ORDER BY annual_salary DESC;
-- Table aliases: shorter names make joins easier to read.
SELECT
e.first_name AS employee_first_name,
d.department_name AS department
FROM employees AS e
JOIN departments AS d
ON e.department_id = d.department_id
ORDER BY employee_first_name;
-- Common failure path:
-- Many databases do NOT let a SELECT alias appear in WHERE at the same query level,
-- because WHERE is evaluated before the SELECT list exists.
-- If you uncomment this, it may fail with an error like:
-- column annual_salary does not exist
-- SELECT salary * 12 AS annual_salary
-- FROM employees
-- WHERE annual_salary > 100000;
-- Correct workaround: move the alias into a subquery, then filter outside it.
SELECT
payroll.employee_id,
payroll.first_name,
payroll.annual_salary
FROM (
SELECT
employee_id,
first_name,
salary * 12 AS annual_salary
FROM employees
) AS payroll
WHERE payroll.annual_salary > 100000
ORDER BY payroll.annual_salary DESC;Follow-up & Tricky Questions:
WHERE runs before the SELECT list is produced, so the alias does not exist yet; use a subquery or CTE if you need to filter on the computed value.ORDER BY runs after SELECT, so the final output name is available for sorting.FROM normally needs an alias so the outer query has a name to reference.Tricky / Gotcha Questions:
salary * 12 AS annual_salary, can I filter with HAVING annual_salary > 100000? Sometimes a dialect will allow it, but it is not the safest portable habit. The clean approach is to use a subquery or CTE, especially if no GROUP BY is involved.AS change behavior? No. AS is mostly for readability; the alias is what matters. Many engines let you omit AS, but keeping it makes the query easier to scan.Common Mistakes:
Memory Hook: Think: alias = sticky note, not a new file. The database keeps the real name in the drawer; the alias is just the label you see during this one query.
Cheat Sheet:
AS for clarity, even when your database allows it to be omitted.WHERE usually cannot see a SELECT alias.ORDER BY usually can see a SELECT alias.FROM usually need an alias.Practice Tasks:
total_price and customer_name.o and c instead of full table names.