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?
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 Expression | Alias | Use Case |
|---|---|---|
salary * 0.1 | bonus | Calculated field |
CONCAT(first, ' ', last) | full_name | String operation |
DATEDIFF(NOW(), hire_date) | tenure_days | Date calculation |
CASE WHEN ... END | status_label | Conditional 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 Practice | Why It Matters |
|---|---|
| Use descriptive aliases | Makes results self-documenting |
| Keep aliases short | Reduces query clutter |
| Use table aliases in joins | Prevents column ambiguity |
| Avoid reserved keywords | Prevents syntax errors |
| Use quotes for spaces | Handles 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
| Topic | Syntax | Example |
|---|---|---|
| Column Alias | SELECT col AS alias | SELECT salary AS pay |
| Table Alias | FROM table AS alias | FROM employees e |
| Expression Alias | SELECT expr AS alias | SELECT price * qty AS total |
| No AS Keyword | SELECT col alias | SELECT 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
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- 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
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- 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
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids 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