Javatpoint Logo
Javatpoint Logo

SQL Server Alias

We can assign a temporary name to the column or expression while executing a SQL Server alias query. SQL Server Alias is beneficial when we want to display some specific data from a SQL Server table to label that data in a mannered way for better data representation. SQL Server Aliases are also used to increase the readability of the columns.

Syntax of SQL Server Alias

SELECT name_of_column AS name_of_alias FROM name_of_table;

In the syntax written above:

  • Name_of_column: The name_of_column represents the name of the column to which we want to assign an alias.
  • Name_of_alias: The name_of_alias represents the name of an alias that we will give to the specified column of the table.
  • Name_of_table: The name_of_table shows the name of the table.

The syntax for Table Alias:

SELECT name_of_column FROM name_of_table AS name_of_alias;

In the syntax written above:

  • Name_of_column: The name_of_column represents the name of the column to which we want to assign an alias.
  • Name_of_alias: The name_of_alias represents the name of an alias that we will give to the specified column of the table.
  • Name_of_table: The name_of_table shows the name of the table.

How to use Alias in SQL Server?

Let us understand the usage and working of the Alias in SQL Server with the help of an example. Let us create a table named Products having three columns named prodID, prodName, and quantity to store the product's ID, name of the product, and quantity, respectively. The command for creating a table in SQL server with the schema mentioned above will be:


SQL Server Alias

As shown in the image, we have successfully created a table with the schema mentioned above: three columns, two(prodID and prodName) columns having data type as varchar, and one(quantity) column as int.

Now, let us add some data to the Products table. The syntax of the INSERT command in SQL Server to add data in the students' table will be:


SQL Server Alias
SQL Server Alias

As we can see in the image, we have successfully added nine rows of data to the Products table, and the same can be seen in the result of the SELECT query.

Now if we want to display the prodName column of the Products table and want to give it a specific name, we can use SQL Server Alias for this and the syntax for displaying a specific column of the Products table under a specific name is:

SELECT prodName AS list_of_products FROM Products;

SQL Server Alias

As shown in the image, the prodName column is displayed under the name 'list_of_products' name with the help of the SQL Server Alias.

The same can be done with two columns also means we can display more than one column with some alternative name using the SQL server alias. The syntax for displaying more than one column as alternative names in SQL server using an alias is:

SELECT prodName AS List, quantity AS Number From products;

SQL Server Alias

As we can see in the image clearly, the two rows ( prodName and quantity) of the Products table are displayed under the alternative names List and Number respectively, with SQL Server Alias.

The above example is of Column Alias, but Table alias can also be used to give alternative names to the tables while using join operations on the table when more than one table is associated in a single SQL query. Then those tables can be referenced using those aliases or alternative names.

So, this article helps us to understand the working of the Alias in SQL Server and how to use it according to our problem requirement.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA