TRENDING TOPICS

×

SQL Aliases

SQL Aliases used to assign the temporary names to table OR column name in a table.

  • Aliases reduce the amount of typing required to enter a query.
  • Alias only exists for the duration of the query.

Alias Table Syntax

SELECT  column_name(s)
FROM table_name
AS alias_name
WHERE [condition];

Alias Column Syntax

SELECT  column_name AS alias_name
FROM table_name
WHERE [condition];

EXAMPLE: SQL Aliases

Student Table

A table of student records, the name of table is "Student", and the name of columns are [ROLL_NO, STUDENT_NAME, ADDRESS, MOBILE_NO, AGE, and BRANCH]:

ROLL_NO STUDENT_NAME ADDRESS MOBILE_NO AGE BRANCH
1 Mohit Sharma Delhi XXXXXXXX87 20 IT
2 Pooja Pandit Kolkata XXXXXXXX34 18 CSE
3 Priya Pandey Delhi XXXXXXXX62 19 EEE
4 Mohit Kumar Jha Allahabad U.P XXXXXXXX87 20 ECE
5 Shevani Shree Noida U.P XXXXXXXX51 17 IT
6 Badshah Mumbai XXXXXXXX02 20 Civil
7 Viki Mohan Pune XXXXXXXX72 19 CSE
8 Sona Kaur Indor M.P XXXXXXXX81 22 ME

Alias for Column Example

SELECT  STUDENT_NAME AS S_NAME, BRANCH AS STUDENT_BRANCH
FROM Student;

Result Lokk Like

S_NAME STUDENT_BRANCH
Mohit Sharma IT
Pooja Pandit CSE
Priya Pandey EEE
Mohit Kumar Jha ECE
Shevani Shree IT
Badshah Civil
Viki Mohan CSE
Sona Kaur ME

If you want space between the Alias Name contains then double quotation marks or square brackets required, write alias Column Name Between:

Alias for Column Example

SELECT  STUDENT_NAME AS S_NAME, BRANCH AS [STUDENT BRANCH]
FROM Student;

Result Lokk Like

S_NAME STUDENT BRANCH
Mohit Sharma IT
Pooja Pandit CSE
Priya Pandey EEE
Mohit Kumar Jha ECE
Shevani Shree IT
Badshah Civil
Viki Mohan CSE
Sona Kaur ME

EXAMPLE: Alias for Tables

Customers Table

Simple Customers record table & the name of the table is CUSTOMERS. The Column(s) name of the table like [ID, NAME, EMAIL, ADDRESS, and SALARY]:

ID NAME EMAIL ADDRESS SALARY
1 Mark JK mark@gmail.com Mumbai 5000
2 Nitika nitika@gmail.com Delhi 6700
3 Suneel Kumar kumars@gmail.com Pune 9400
4 Jenifer jenifer@gmail.com Indore 3400
5 McKinley mckinley@gmail.com UK 7200
6 Deepika deepika@gmail.com Nashik 3600

Orders Table

This is a sample ORDER table; The columns of the table are [OID, DATE, CUSTOMER_ID, and AMOUNT]:

OID DATE CUSTOMER_ID AMOUNT
210 12/01/2019 2 4000
209 11/22/2019 4 2000
211 12/10/2019 1 1000
212 12/17/2019 6 3000

The table aliases are used to rename a table in a specific SQL statement to make it easier to read.

Alias for Tables Example

SELECT  C.ID, C.NAME, C.EMAIL, O.AMOUNT
FROM CUSTOMERS AS C, ORDERS AS O
WHERE C.ID = O.CUSTOMER_ID;

Result Lokk Like

ID NAME EMAIL AMOUNT
2 Nitika nitika@gmail.com 4000
4 Jenifer jenifer@gmail.com 2000
1 Mark JK mark@gmail.com 1000
6 Deepika deepika@gmail.com 3000

Advertise With Us

We offer sponsored posts and guest posts with do-follow links, back-links, and other advertising services, To advertise email us at "advertise@gdatamart.com"

© 2021 GDATAMART.COM (All Rights Reserved)