🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Aliases (AS)

SQL FundamentalsDMLđŸŸĸ Free Lesson
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?

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;

Column Aliases

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

-- 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.

-- 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;
-- 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

-- 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

-- 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;

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

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

Extended Learning: Beyond the Basics

Key Concepts Summary

This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.

Prerequisite Knowledge

Before studying this topic in depth, ensure you understand:

  • Basic definitions and terminology
  • Core formulas and their derivations
  • Common applications in real-world scenarios
  • Typical pitfalls and misconceptions

Step-by-Step Walkthrough

Let us work through a complete example to solidify understanding:

Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.

Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.

Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.

Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.

Common Mistakes to Avoid

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. Forgetting constraints — Check that solutions satisfy all given conditions

Real-World Applications

Understanding this concept is essential for:

  • Advanced problem solving in examinations
  • Building intuition for more complex topics
  • Developing analytical thinking skills
  • Applying mathematical reasoning to practical situations

Practice Exercises

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. Challenge: Create your own problem that requires this concept plus additional knowledge.

Interview and Exam Tips

  • Common question patterns: Identify which exam or interview formats test this topic
  • Time management: Practice completing problems within time constraints
  • Common traps: Know the typical mistakes examiners set as distractors
  • Verification strategy: Develop a quick method to check your answer

Related Topics

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids errors

Further Reading

For students who want to go deeper:

  • Standard textbooks on the subject
  • Research papers applying this concept
  • Online courses with video explanations
  • Practice problem collections with detailed solutions
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement