×

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 Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 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 INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

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 INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

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.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 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 INSERT Table

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

5 minutes read.