codingstreets
Search
Close this search box.
sql-aliases

Get Started: SQL Aliases

In This Article, You Will Know About SQL Aliases. 

SQL Aliases Operator – Before moving ahead, let’s know a bit about SQL BETWEEN Operator.

Table of Contents

SQL Aliases

SQL Aliases – SQL Aliases are used to give a temporary name to a table and column that is valid for a particular SQL Query. Aliases are used when SQL has to use another name rather than the original name of the table or column.

  • Aliases – It makes the table and column name easier to read when the big and difficult name is given to the table or column.
  • The temporary name is just a temporary change in the table name. The table name remains original in the database.
  • Aliases are good to use when there is more than one table in SQL Query.

Table Syntax

				
					SELECT column_name(s)
FROM table_name AS alias_name;
				
			

Column Syntax

				
					SELECT column_name AS alias_name
FROM table_name;
				
			

Example Database

Below is the example of a database table named “details.”

IdNameClasssection_id
1Alex51
2Rolex62
3Salex72
4Lelex83
5Kelex95

Alias – Columns Examples

Example: Use SQL Aliases to give the temporary name to the column.

				
					SELECT Name AS N, Class AS C
FROM details;
				
			

As a result, it returns the query from the column Name AS N, Class AS C.

Example: Use SQL Aliases to give the temporary name to the column.

				
					SELECT Name AS N, Class AS 'C'
FROM details;
				
			

As a result, it returns the query from the column Name AS N, Class AS C.

Note: It is required to mention double-quotation marks or square brackets if the alias name contains spaces.

Sample Data

Below is the example of a database table named “Information”

Name

Address

City

State

Alex

10/322

New Delhi

Delhi

John

514/67

Pune

Maharashtra

Alen

D-2, 54

Panji

Goa

Example: Use SQL Aliases to give the temporary name to the column.

				
					SELECT Name, Address + ', ' + City + ', ' + State AS Records
FROM details; 
				
			

As a result, it returns the combined address after adding creating an alias “Records” that adds three columns “Address”, “City”, and “State”.

Alias – Table Example

				
					SELECT n.Name, c.Class. s.state
From details AS D, Information AS I;	
				
			

As a result, it returns the combined data including both tables and all columns.

Here alias is used with columns name to make it easy to read columns’ names.

Alias is useful when:

  • There are multiple tables in the query
  • Functions are employed in the query.
  • The column names are large or difficult to read
  • More than one column may be joined.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on