What is an ALIAS command in SQL Server, where apply alias in the database command?

What is an ALIAS command in SQL Server, where apply alias in the database command?

Last updated:9/22/2021 1:20:51 AM

1 Answers

Ashutosh Kumar Verma
Ashutosh Kumar Verma

SQL Aliases :

SQL aliases are used to give a temporary name to tables or columns in table. It is exist only duration of that query in perform. Alias are used to provide a easier name to table or column of table. An alias is create with use of ‘AS’ keyword.

Syntax :

Alias for column-

SELECT column_name AS alias_name FROM table_name;

We can also create alias for multiple column in a table.

SELECT column1 AS alias1, column2 AS alias2 ,… FROM table_name ;

Alias for Table-

SELECT colum1,column2,… FROM table_name AS alias_name ;

Example :

Lets have a table ‘Customer’,

What is an ALIAS command in SQL Server, where apply alias in the database command?

Follow the statement is used alias for table column ‘cust_name’ as ‘name’ and ‘city’ as ‘c’ .

select cust_name as name, city as c from Customer;

What is an ALIAS command in SQL Server, where apply alias in the database command?

The following SQL statement is using alias for a table ‘Customer’ as ‘cust’.

select cust.cust_name,cust.city,cust.customer_id from Customer as cust;

What is an ALIAS command in SQL Server, where apply alias in the database command?

We can also provide aliases to retrieve record from more than one table at a time like below.

select cust.cust_name,cust.city,cust.customer_id, sale.salesman_id, sale.name 

from Customer as cust,Salesman as sale where cust.salesman_id=sale.salesman_id;

What is an ALIAS command in SQL Server, where apply alias in the database command?

We can also provide single alias for two or more columns in a table at a time. In the following statement a alias ‘name’ for column name ‘cust_name’ and ‘city’ in ‘Customer’ table.

 select customer_id, cust_name+','+city as name from Customer;

What is an ALIAS command in SQL Server, where apply alias in the database command?

To give space between alias name use [alias name] in SQL statement.

select customer_id, cust_name+' , '+city as [name  address] from Customer;

What is an ALIAS command in SQL Server, where apply alias in the database command?

In the following statement select a record where customer_id=3007(Brad Davis). By using ‘Customer’ and ‘Salesman’ table and give the alias name as ‘cust’ and ‘sale’ respectively.

select cust.customer_id, cust.cust_name,sale.salesman_id,sale.name 

from Customer as cust, Salesman as sale
where cust.cust_name='Brad Davis' AND cust.salesman_id=sale.salesman_id;

What is an ALIAS command in SQL Server, where apply alias in the database command?

Answer