Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Use Column Alias in SELECT Statement?

  • Last Updated : 08 Oct, 2021

Alias is used to give a temporary name(only for the duration of the query) to the column or table in order to make the column name or table name more readable. It does not change the name of the column permanently. 

Alias can be performed using the ‘AS’ keyword or without any keyword. But it is recommended to use the ‘AS’ keyword to avoid confusion between the initial column name and column alias.

If alias name contain space or any special character, enclose alias name in single(‘ ‘) or double(” “) quotes.

In SQL strings are also enclosed in single(‘ ‘) quotes. To avoid confusion,  it is recommended to use double quotes for alias name.

Syntax for Table Alias:

SELECT * FROM table_name AS alias_name;

Syntax for Column Alias:

SELECT column_name AS alias_name FROM table_name;

For the purpose of the demonstration, we will be creating demo_table in a database called “geeks“.

Step 1: Creating the Database

Use the below SQL statement to create a database called geeks.

Query:

CREATE DATABASE geeks;

Step 2: Using the Database

Use the below SQL statement to switch the database context to geeks.

Query:



USE geeks;

Step 3: Table definition

We have the following demo_table in our geeks database.

Query:

CREATE TABLE demo_table
(FIRSTNAME VARCHAR(20),
ENDNAME VARCHAR(20),
AGE INT,GENDER VARCHAR(20));

Step 4: Insert data into the table

INSERT INTO demo_table VALUES
('Romy', 'Kumari', 22, 'female'),
('Meenakshi', 'Jha', 20, 'female'),
('Shalini', 'Jha', 22, 'female'),
('Akanksha', 'Gupta', 23, 'female'),
('Rinkle', 'Arora', 23, 'female');

Step 5: See the content of the table

Use the below command to see the content of the demo_table:

Query:

SELECT * FROM demo_table;

Step 6: Perform column alias on ENDNAME column.

We will use LASTNAME as alias name for ENDNAME column 



Query:

SELECT FIRSTNAME, ENDNAME AS LASTNAME, AGE, GENDER FROM demo_table;

Output:

Use (LAST NAME) and (FIRST NAME) as alias name 

If we have a special character or spaces in the alias name, we enclose the alias name within double brackets:

Query:

SELECT FIRSTNAME AS "FIRST NAME", ENDNAME AS "LAST NAME", AGE, GENDER FROM demo_table;

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!