Published on

February 3, 2020

Understanding SQL Server AS Keyword

The SQL AS keyword is a powerful tool that allows us to give aliases to table and column names in queries. By using aliases, we can greatly improve the readability and understandability of our queries and result sets.

Why Use Aliases?

When writing complex queries with multiple joins and long table and column names, using aliases can make the code more concise and easier to work with. Aliases provide a temporary name for table and column headings in the result set, making them more meaningful and clear.

Syntax for Column Aliases

To give an alias to a column, we use the following syntax:

SELECT column_name AS alias_name
FROM table_name;

For example, if we want to give aliases to the “CustomerInfList_FirstName” and “CustomerInf_LastName” columns in the “Customer_InformationListForSale” table, we can write:

SELECT CustomerInfList_FirstName AS FirstName, CustomerInf_LastName AS LastName
FROM Customer_InformationListForSale;

Syntax for Table Aliases

When a query involves a table more than once, using aliases for the tables can be very useful. The syntax for table aliases is as follows:

SELECT column_name
FROM table_name AS table_alias;

For example, if we want to give aliases to the “Customer_InformationListForSale” and “OrderTransaction_InformationListForSale” tables, we can write:

SELECT Customer.CustomerInfList_FirstName AS FirstName, Customer.CustomerInf_LastName AS LastName, CustomerOrders.Amout AS OrderAmount
FROM Customer_InformationListForSale AS Customer
INNER JOIN OrderTransaction_InformationListForSale AS CustomerOrders
ON Customer.ID = CustomerOrders.Customer_InformationListForSale_CustID;

By using table aliases, we can avoid retyping long table names and make our queries more concise and readable.

Conclusion

In this article, we have explored the usage of the SQL AS keyword for giving aliases to table and column names. By using aliases, we can improve the readability of our queries, reduce complexity, and make our code more maintainable. The AS keyword allows us to give more meaningful column headings and shorten table names when using them multiple times in a query.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.