MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The AS
keyword in MySQL is used to assign an alias (temporary name) to a table or a column in a table. This can be used for making column names more readable and for shortening table names to make queries easier to write.
Here's a basic usage of the AS
keyword:
Column Aliases
Suppose you have a table called employees
with a column called employee_name
. You can use AS
to give employee_name
a more friendly name when you retrieve it:
SELECT employee_name AS name FROM employees;
In this case, the employee_name
column will be returned as name
.
Table Aliases
Table aliases are useful for shortening queries and for performing joins. Suppose you have a table called employees
. You can use AS
to give employees
a shorter alias when you retrieve data:
SELECT e.employee_name FROM employees AS e;
In this case, employees
is given the alias e
, so you can use e
instead of employees
in your query.
Aliases in Joins
Aliases are particularly useful in JOIN operations where you're working with multiple tables. Suppose you have two tables: employees
and departments
. You can use AS
to give each table an alias to make the JOIN easier to write:
SELECT e.employee_name, d.department_name FROM employees AS e JOIN departments AS d ON e.department_id = d.department_id;
In this case, employees
is given the alias e
and departments
is given the alias d
, so you can use e
and d
instead of the full table names in your query.
Note: The AS
keyword is optional. You can assign an alias without using AS
. The following is equivalent to the above query:
SELECT e.employee_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;
In either case, the alias only exists for the duration of the query.
Set alias in MySQL SELECT statement:
SELECT column_name AS alias_name FROM your_table;
Creating aliases with AS in MySQL:
AS
keyword to create aliases for columns or expressions in the SELECT statement.SELECT first_name AS FirstName, last_name AS LastName FROM employees;
Assigning column aliases in MySQL:
SELECT product_name AS Product, unit_price AS Price FROM products;
Use of AS for column naming in MySQL:
AS
keyword is used for assigning aliases to columns, providing a clearer name in the result set.SELECT employee_id AS ID, employee_name AS Name FROM employee_data;
Renaming columns using AS in MySQL:
AS
to rename columns temporarily in the SELECT statement.SELECT category_name AS Category, product_quantity AS Quantity FROM inventory;
MySQL AS in JOIN statements:
AS
in JOIN statements to alias tables and simplify references.SELECT customers.customer_id, orders.order_date FROM customers JOIN orders AS o ON customers.customer_id = o.customer_id;
AS keyword for aliasing in MySQL queries:
AS
keyword is used for aliasing, providing a convenient way to rename columns or tables.SELECT order_id AS OrderID, order_date AS OrderDate FROM sales_orders;
MySQL AS vs without AS in queries:
AS
keyword is optional when setting aliases in MySQL. You can also omit it and directly provide the alias.AS
:SELECT product_name AS Product, unit_price AS Price FROM products;
AS
:SELECT product_name Product, unit_price Price FROM products;
Examples of using AS to set aliases in MySQL:
AS
keyword to set aliases in MySQL queries.SELECT employee_id AS ID, employee_name AS Name FROM employee_data; SELECT category_name AS Category, product_quantity AS Quantity FROM inventory;