SQL Aliases: Using Aliases in SQL Queries

Rumman Ansari   Software Engineer   2024-07-18 09:40:26   5805  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Syntax: Alias Column Syntax


SELECT column_name AS alias_name
FROM table_name

Syntax: Alias Table Syntax


SELECT column_name(s)
FROM table_name AS alias_name

Prerequisite Codes

If you don't have table please create below table and practice it.

My database name is SQLExamples



USE SQLExamples

Create this below Employee table


DROP TABLE Employee
CREATE TABLE Employee(
EmpId INT,
EmpName VARCHAR(25),
EmpAddress VARCHAR(100),
EmpDept VARCHAR(25)
)

Insert data inside table


INSERT INTO Employee VALUES
(1, 'Rambo', 'Kolkata', 'IT'),
(2, 'Rohit', 'Kolkata', 'IT'),
(3, 'Rohon', 'Kolkata', 'ITIS'),
(4, 'Ronok', 'Kolkata', 'ITIS'),
(5, 'Rubin', 'Kolkata', 'ITIS'),
(6, 'Sorif', 'Kolkata', 'ADMIN'),
(7, 'Soriful', 'Kolkata', 'ADMIN'),
(8, 'Sofik', 'Kolkata', 'ADMIN')



SELECT * FROM Employee

Output:

The above code will produce the following result-

EmpId

EmpName

EmpAddress

EmpDept

1

Rambo

Kolkata

IT

2

Rohit

Kolkata

IT

3

Rohon

Kolkata

ITIS

4

Ronok

Kolkata

ITIS

5

Rubin

Kolkata

ITIS

6

Sorif

Kolkata

ADMIN

7

Soriful

Kolkata

ADMIN

8

Sofik

Kolkata

ADMIN

Example:

Code: Alias Column Syntax


SELECT EmpAddress AS cityName
FROM Employee

Output:

The above code will produce the following result-

cityName

Kolkata

Kolkata

Kolkata

Kolkata

Kolkata

Kolkata

Kolkata

Kolkata

Example:

Code: Alias Table Syntax


SELECT EmpName
FROM Employee AS EmployeeTable

Output:

The above code will produce the following result-

EmpName

Rambo

Rohit

Rohon

Ronok

Rubin

Sorif

Soriful

Sofik