How can we use alias name in mysql workbench

  • Setting aliases in a `SELECT` query allows you to provide a temporary name for a column or expression, which can be useful for improving readability or performing calculations.

Here's how you can set aliases in a `SELECT` query in MySQL:

  • Basic Column Alias: To set an alias for a column name, use the `AS` keyword followed by the alias name. You can also omit the `AS` keyword and directly specify the alias.

    SELECT column_name AS alias_name
    FROM table_name;

  • Example:

    SELECT first_name AS "First Name", last_name AS "Last Name"
    FROM employees;

  • Alias for Expressions: You can also set an alias for expressions or calculations within the query.

    SELECT column1 * 1.10 AS "Increased Price"
    FROM products;

  • Using Table Aliases: You can set an alias for a table name, which is useful when you're joining multiple tables. This can simplify referencing columns from the table.

    SELECT e.first_name, e.last_name, d.department_name
    FROM employees AS e
    JOIN departments AS d ON e.department_id = d.department_id;

  • Using Aliases with Aggregate Functions: Aliases can be useful when using aggregate functions to provide more meaningful names to the calculated values.

    SELECT AVG(salary) AS "Average Salary"
    FROM employees;

  • Column Aliases without AS: While it's common to use `AS` for aliasing, you can often omit it and directly use the alias name after the column or expression. This works in most cases.

    SELECT column_name alias_name
    FROM table_name;


Remember these points when using aliases:

  • You can use single quotes (`'`) or double quotes (`"`) to enclose alias names that contain spaces or special characters.
  • Aliases only affect the output presentation and do not change the column or table names in the database.
  • Setting aliases in a `SELECT` query can make your query results more readable and intuitive, especially when dealing with complex expressions, calculations, or table joins.

No comments:

Post a Comment