MySQL Alias

MySQL Alias

MySQL Alias

An alias in MySQL is a temporary name given to a table or column for the duration of a query. Aliases make queries more readable, especially when dealing with complex expressions or joining multiple tables.


1. Column Alias

A column alias renames a column in the result set, making interpretation easier.

Syntax

SELECT column_name AS alias_name FROM table_name;
  • AS is optional but improves clarity.
  • Aliases are only for display purposes; they do not change the column name in the table.

Example

Without Alias
SELECT first_name, last_name, CONCAT(first_name, ' ', last_name) FROM employees;
With Alias
SELECT first_name, last_name, CONCAT(first_name, ' ', last_name) AS full_name FROM employees;

Result

+------------+-----------+-----------+ | first_name | last_name | full_name | +------------+-----------+-----------+ | Alice | Johnson | Alice Johnson | | Bob | Smith | Bob Smith | +------------+-----------+-----------+

2. Table Alias

A table alias renames a table within a query, often for shorter references in complex queries, especially with joins or subqueries.

Syntax

SELECT column_name FROM table_name AS alias_name;
  • The alias can be used to reference the table elsewhere in the query.

Example

Without Alias
SELECT employees.first_name, departments.department_name FROM employees JOIN departments ON employees.department_id = departments.id;
With Alias
SELECT e.first_name, d.department_name FROM employees AS e JOIN departments AS d ON e.department_id = d.id;

Result

+------------+----------------+ | first_name | department_name | +------------+----------------+ | Alice | IT | | Bob | HR | +------------+----------------+

3. Using Aliases with Calculations

Aliases are helpful for calculations to name the derived columns.

Example

Calculate total compensation (salary + bonus) for employees:

SELECT first_name, salary, bonus, (salary + bonus) AS total_compensation FROM employees;

Result

+------------+--------+-------+-------------------+ | first_name | salary | bonus | total_compensation| +------------+--------+-------+-------------------+ | Alice | 50000 | 5000 | 55000 | | Bob | 45000 | 4500 | 49500 | +------------+--------+-------+-------------------+

4. Aliases in Subqueries

Aliases are required when using subqueries, as they provide a name for the derived table.

Example

Find employees with a salary higher than the department average:

SELECT e.first_name, e.salary FROM employees AS e JOIN ( SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id ) AS dept_avg ON e.department_id = dept_avg.department_id WHERE e.salary > dept_avg.avg_salary;

5. Aliases with Aggregate Functions

When using aggregate functions like SUM, AVG, COUNT, etc., aliases make the output more meaningful.

Example

Count the number of employees in each department:

SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id;

Result

+---------------+----------------+ | department_id | employee_count | +---------------+----------------+ | 1 | 10 | | 2 | 15 | +---------------+----------------+

6. Aliases Without AS

The AS keyword is optional in MySQL, but using it is considered a good practice for better readability.

Example

SELECT first_name full_name FROM employees;

This works, but the alias might be harder to identify without AS.

7. Best Practices for Aliases

  1. Use Meaningful Aliases:

    • Avoid single letters unless it improves clarity (e.g., table aliases in joins).
    • Example: SELECT d.name AS department_name instead of SELECT d.name AS dn.
  2. Consistency:

    • Use the same alias throughout the query to avoid confusion.
  3. Escape Special Characters:

    • Use backticks (`) if the alias contains spaces or special characters.
    • Example:
      SELECT first_name AS `Employee Name` FROM employees;

Let me know if you'd like additional examples or clarifications!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close