SQL Aliases
Aliases are temporary names you assign to columns or tables to make query results cleaner or easier to read.
They are especially useful in:
- Renaming column headers in output
- Using shorter table names (especially with JOINs)
- Improving readability
🔹 Column Alias Syntax
SELECT column_name AS alias_name
FROM table_name;
You can also omit AS
:
SELECT column_name alias_name
FROM table_name;
✅ Aliases with spaces must be in double quotes or square brackets.
📌 Example: Column Alias
SELECT
first_name AS "First Name",
salary AS income
FROM employees;
🔹 Table Alias Syntax
SELECT t.column1, t.column2
FROM table_name AS t;
Also works without AS
:
SELECT t.column1, t.column2
FROM table_name t;
📌 Example: Table Alias in JOIN
SELECT e.employee_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
✅ Makes long queries much cleaner.
🧠 Quick Recap
Key Point | Explanation |
---|---|
Column Alias | Temporarily rename output column headers |
Table Alias | Shorten table names for cleaner syntax |
AS Optional | AS keyword is optional, but improves clarity |
Quotes Needed | Use quotes for aliases with spaces or special chars |
💡 Use aliases to write cleaner, clearer SQL – especially in joins and reports.