The "AS" keyword is a useful tool for developers, as it allows them to give temporary names to table columns or even entire tables. This temporary renaming, also called aliasing, makes query results easier to read and understand without making permanent changes to the database structure.
Let's start by learning the syntax of SQL SELECT AS. It is a simple but powerful structure.
SELECT Column_Name1 AS New_Column_Name, Column_Name2 AS New_Column_Name FROM Table_Name;
Here, Column_Name" refers to the original name of a column, while "New_Column_Name" is a temporary alias given to that column for a specific query.
Consider a table called 'Order_Detail' with columns such as 'Day' 'Customer_Name' 'Product_Name' and 'Quantity'. I f you want to improve clarity, you can rename 'Day' to 'Order_Date' and 'Customer_Name' to 'Client_Name' The query would appear as follows:
SELECT day_of_order AS 'Date', Customer AS 'Client', Product, Quantity FROM orders;
Example 2: Combining Columns with CONCAT()
To merge 'Student_RollNo' with 'Student_PhoneNumber' and 'Student_HomeTown' and assign temporary names 'Roll No' and 'Student_Info', use the CONCAT() function in the query.
SELECT Student_RollNo AS 'Roll No', CONCAT(Student_PhoneNumber, ', ', Student_HomeTown) AS Student_Info FROM students;
This demonstrates how SQL SELECT AS can be combined with other functions to achieve more complex transformations.
Alias for Tables - Beyond Columns
SQL SELECT AS allows creating aliases for both columns and tables, which is beneficial for complex queries involving multiple tables.
Example 3: Creating an Alias for the 'Students' Table
SELECT s.Student_RollNo, s.Student_Name, s.Student_Gender, s.Student_PhoneNumber, s.Student_HomeTown FROM students AS s WHERE s.Student_RollNo = 3;
Here, 's' acts as a temporary alias for the 'students' table, simplifying the query.