πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Aliases (AS)

SQL FundamentalsDML🟒 Free Lesson

Advertisement

SQL Fundamentals

Aliases (AS)

Give temporary names to columns and tables using the AS keyword for cleaner, more readable queries.

  • Column Aliases β€” Rename result columns for clarity
  • Table Aliases β€” Shorten table references in joins
  • Expression Aliases β€” Label calculated fields Make your queries self-documenting with meaningful names.

What Is an Alias?

DfAlias (AS)

An alias is a temporary name assigned to a column or table within a SQL query. It does not modify the underlying database schema β€” it only changes how the name appears in the query result or within the query itself.

Alias ConceptOriginal Column Namesfirst_namesalary * 0.1CONCAT(f, ' ', l)Hard to readASRenameAliased ResultFirst NameBonus AmountFull NameSelf-documentingTypesColumnTableExprView
SELECT
    first_name AS "First Name",
    last_name AS "Last Name",
    salary * 0.1 AS "Bonus Amount"
FROM employees;
The AS keyword is optional for column aliases but required when the alias contains spaces or special characters. Table aliases always omit AS for brevity.

Column Aliases

Column aliases rename columns in the output. They improve readability when working with calculated fields or verbose column names.

DfColumn Alias Syntax

SELECT expression AS alias_name FROM table_name; The alias takes effect only for the duration of the query.

-- Basic column renaming
SELECT
    first_name AS employee_first,
    last_name AS employee_last,
    salary AS annual_salary
FROM employees;
-- Aliases for calculated expressions
SELECT
    product_name,
    unit_price,
    quantity,
    unit_price * quantity AS line_total,
    ROUND(unit_price * quantity, 2) AS formatted_total
FROM order_items;
-- String concatenation with alias
SELECT
    CONCAT(first_name, ' ', last_name) AS full_name,
    email AS contact_email
FROM customers;
Original ExpressionAliasUse Case
salary * 0.1bonusCalculated field
CONCAT(first, ' ', last)full_nameString operation
DATEDIFF(NOW(), hire_date)tenure_daysDate calculation
CASE WHEN ... ENDstatus_labelConditional logic

Table Aliases

Table aliases shorten table names, especially useful in multi-table joins where the same table appears multiple times.

DfTable Alias

A table alias assigns a short identifier to a table within a query. It is essential for self-joins and improves readability in complex joins.

-- Table aliases in a join
SELECT
    c.first_name,
    c.last_name,
    o.order_date,
    o.total_amount
FROM customers AS c
INNER JOIN orders AS o ON c.id = o.customer_id;
-- Self-join using table aliases
SELECT
    e.first_name AS employee,
    m.first_name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;
-- Multi-table join with aliases
SELECT
    c.customer_name,
    o.order_date,
    p.product_name,
    oi.quantity,
    oi.unit_price
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
INNER JOIN order_items oi ON o.order_id = oi.order_id
INNER JOIN products p ON oi.product_id = p.product_id;
You cannot use column aliases in the WHERE clause because WHERE is evaluated before SELECT. Use the original column name or a HAVING clause for aggregate aliases.
-- This FAILS: alias 'emp_salary' not recognized in WHERE
SELECT first_name AS emp_salary FROM employees WHERE emp_salary > 50000;

-- Correct: use the original column name
SELECT first_name AS emp_salary FROM employees WHERE salary > 50000;

-- Correct: use HAVING for aggregate aliases
SELECT department_id, COUNT(*) AS emp_count
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;

Alias Rules and Best Practices

DfAlias Naming Rules

  • Aliases can contain letters, numbers, and underscores
  • Avoid reserved keywords as aliases
  • Use double quotes for aliases with spaces or special characters
  • Alias names are case-insensitive in most databases
-- Good alias practices
SELECT
    e.first_name AS first,           -- Short and clear
    e.last_name AS last,
    d.name AS department,            -- Meaningful name
    e.salary AS salary               -- Redundant but valid
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;
-- Avoid cryptic aliases
-- Bad:
SELECT a, b, c FROM x JOIN y ON a = d;

-- Good:
SELECT e.first_name, e.salary, d.name
FROM employees e
JOIN departments d ON e.dept_id = d.id;
Best PracticeWhy It Matters
Use descriptive aliasesMakes results self-documenting
Keep aliases shortReduces query clutter
Use table aliases in joinsPrevents column ambiguity
Avoid reserved keywordsPrevents syntax errors
Use quotes for spacesHandles special characters

Performance Impact

DfAlias Performance

Aliases have zero performance impact. The database optimizer works with original column and table names internally. Aliases are purely a display and readability feature.

-- Both queries perform identically
SELECT first_name AS name, salary AS pay FROM employees WHERE salary > 50000;
SELECT first_name, salary FROM employees WHERE salary > 50000;
Indexes are matched against original column names, not aliases. The query planner resolves aliases before execution, so there is no runtime cost.

Practice Exercises

Exercise 1: Write a query using table aliases to join customers with orders and display the customer name, order date, and total.

-- Solution
SELECT
    c.first_name,
    c.last_name,
    o.order_date,
    o.total AS order_total
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id;

Exercise 2: Create aliases for complex calculations in an employee bonus report.

-- Solution
SELECT
    employee_id,
    first_name,
    last_name,
    salary,
    salary * 0.1 AS bonus,
    salary + (salary * 0.1) AS total_compensation,
    ROUND(salary * 12, 2) AS annual_salary
FROM employees;

Exercise 3: Use table aliases in a three-table join between customers, orders, and products.

-- Solution
SELECT
    c.first_name AS customer,
    o.order_date,
    p.product_name,
    oi.quantity,
    oi.unit_price
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id
INNER JOIN order_items oi ON o.id = oi.order_id
INNER JOIN products p ON oi.product_id = p.id;

Summary

Key Takeaways

  1. Column aliases rename columns in the result set for readability
  2. Table aliases shorten table references, essential for self-joins
  3. The AS keyword is optional for columns and tables
  4. Aliases cannot be used in WHERE β€” use the original column name
  5. Aliases have no performance impact on query execution
  6. Use meaningful alias names to make queries self-documenting
TopicSyntaxExample
Column AliasSELECT col AS aliasSELECT salary AS pay
Table AliasFROM table AS aliasFROM employees e
Expression AliasSELECT expr AS aliasSELECT price * qty AS total
No AS KeywordSELECT col aliasSELECT salary pay
⭐

Premium Content

Aliases (AS)

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement