×

SQL Aliases

SQL Aliases:

These are used to give a temporary name for a column or table. These are used to make tables or columns more readable.

  • These are used to rename the table or column temporarily. But it does not change in the original database.
  • Aliases are used when the table names or column names  are big and they are not readable
  • Alias is used with the “AS” Keyword.
  • It exists only for the duration of a particular query.

Alias Column Syntax:

select columnname AS aliasname from tablename;

Alias Table Syntax:

select columnname from tablename AS aliasname;

Alias Column Example:

Example:

Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Changing one column name by Alias:

select sname AS studentname from student;

Output:

Student Table

studentname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshitha
Bindhu
Nandhini
Hashish
Rahul
Select sid AS studentid from Student;

Output:

Student Table:

studentid
1
2
3
4
5
6
7
8
9
10

Changing two column names by Alias:

Select sid AS studentid, sname AS studentname from student;

Output:

Student Table

studentidstudentname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshitha
7 8Bindhu Nandhini
9Hashish
10Rahul
Select sid AS studentid, sage AS studentage from student;

Output:

Student Table

studentidstudentage
122
224
321
421
523
622
726
823
922
1021

Changing three column names by Alias:

Select sid AS studentid, sname AS studentname , sage AS studentage from student;

Output:

Student Table

studentidstudentnamestudentage
1Abhinav22
2Ramya24
3Preetham21
4Nethranand21
5Naveen23
6Harshita22
7Bindu26
8Nandhini23
9Hashish22
10Rahul21

Using Multiple column names as one column name by Alias:

Method I: select * from Student Table AS Student Data;

SELECT sname , sid + ', ' + sage + ', ' + Phonenumber AS studentdetails
FROM student;

Output:

Student Table

snamestudentdetails
Abhinav1, 22, Male, 9895678909
Ramya2, 24, Female, 6687654634
Preetham3, 21, Male, 9867546453
Nethranand4, 21, Male, 7675643423
Naveen5, 23, Male, 6567784532
Harshita6, 22, Female, 9867546231
Bindu7, 26, Female, 6563412768
Nandhini8, 23, Female, 6785674839
Hashish9, 22, Male, 9453215052
Rahul10, 21, Male, 9998989898

Method II:

Using concat operator

SELECT sname, CONCAT(sid, ', ', sage, ', ', sgender, ‘,’ Phonenumber) AS studentdetails
FROM Customers;

Output:

Student Table

snamestudentdetails
Abhinav1, 22, Male, 9895678909
Ramya2, 24, Female, 6687654634
Preetham3, 21, Male, 9867546453
Nethranand4, 21, Male, 7675643423
Naveen5, 23, Male, 6567784532
Harshita6, 22, Female, 9867546231
Bindu7, 26, Female, 6563412768
Nandhini8, 23, Female, 6785674839
Hashish9, 22, Male, 9453215052
Rahul10, 21, Male, 9998989898

Method III:

SELECT sname, (sid || ', ' || sage || ' ' || sgender||',' || Phonenumber) AS studentdetails from student;

Output:

Student Table

snamestudentdetails
Abhinav1, 22, Male, 9895678909
Ramya2, 24, Female, 6687654634
Preetham3, 21, Male, 9867546453
Nethranand4, 21, Male, 7675643423
Naveen5, 23, Male, 6567784532
Harshita6, 22, Female, 9867546231
Bindu7, 26, Female, 6563412768
Nandhini8, 23, Female, 6785674839
Hashish9, 22, Male, 9453215052
Rahul10, 21, Male, 9998989898

Alias Table Example:

Change Student Table name as Student Data

select * from Student Table AS Student Data;

Output:

      Student Data

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Change Student Table as Student Details

select * from Student Table AS Student Details;

Output:

Student Details

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Related Topics

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.