0
0
MySQLquery~5 mins

Column aliases in MySQL

Choose your learning style9 modes available
Introduction

Column aliases let you give a temporary new name to a column in your query result. This makes the output easier to read and understand.

When you want to rename a column to something more user-friendly in the results.
When you use a calculation or function in a column and want to name the result.
When you join tables and want to avoid confusing column names.
When you want to display a column with a different heading in reports or dashboards.
Syntax
MySQL
SELECT column_name AS alias_name FROM table_name;
The keyword AS is optional but helps make the alias clear.
Alias names can be enclosed in backticks (`) if they contain spaces or special characters.
Examples
This renames the column first_name to name in the output.
MySQL
SELECT first_name AS name FROM employees;
This calculates yearly salary and names the column annual_salary.
MySQL
SELECT salary * 12 AS annual_salary FROM employees;
This renames a column with a space in its name using backticks.
MySQL
SELECT `order date` AS order_date FROM orders;
Sample Program

This creates a simple employees table, inserts two rows, and selects the first name and yearly salary with aliases.

MySQL
CREATE TABLE employees (id INT, first_name VARCHAR(20), salary INT);
INSERT INTO employees VALUES (1, 'Alice', 3000), (2, 'Bob', 4000);
SELECT first_name AS employee_name, salary * 12 AS yearly_salary FROM employees;
OutputSuccess
Important Notes

Aliases only change the column name in the output, not in the actual table.

Use aliases to make your query results clearer for others reading them.

Summary

Column aliases rename columns temporarily in query results.

They improve readability and clarity of output.

Use AS keyword or just a space to assign an alias.