SQL Queries:- Column Aliases, In the series of most commonly asked questions SQL queries in interviews, are shown below, if you want to learn more or have any topic-related doubts please ask below in the given comment section.
Que. Define SELECT Using Column Aliases in SQL Queries
Column aliases are used mainly to shorten code and make column names more readable. OR
SQL aliases are used to give a table, or a column in a table, a temporary name.
Code becomes shorter as long table names and unnecessary identification of columns can be avoided. Along with table aliases, this allows you to use longer descriptive names in your database structure while keeping queries upon that structure concise.
How we can create column aliases in all versions of Microsoft SQL Server
We can create Aliases in all versions of SQL using double quotes (“).
SELECT
FName AS "First Name",
MName AS "Middle Name",
LName AS "Last Name"
FROM STUDENTSIn the Case of Different Versions:-
We can use single quotes (‘), double quotes (“), and square brackets ([]) to create an alias in Microsoft SQL Server.
SELECT
FName AS "First Name",
MName AS 'Middle Name',
LName AS [Last Name]
FROM STUDENTSCheck Both will result in:
| First Name | Middle Name | Last Name |
| James | John | Smith |
| John | James | Johnson |
| Michael | Marcus | Williams |
The statement will return FName and LName columns with a given name (an alias). This is achieved using the AS operator followed by the alias, or simply writing the alias directly after the column name. This means that the following query has the same outcome as the above.
SELECT
FName "First Name",
MName "Middle Name",
LName "Last Name"
FROM STUDENTS| First Name | Middle Name | Last Name |
| James | John | Smith |
| John | James | Johnson |
| Michael | Marcus | Williams |
If the alias has a single word that is not a reserved word, we can write it without single quotes, double quotes or brackets:
SELECT
FName AS FirstName,
LName AS LastName
FROM Employees| First Name | Last Name |
| James | Smith |
| John | Johnson |
| Michael | Williams |
Read Also: Most commonly asked SQL Queries Part 2
Most commonly asked SQL Queries Part 1
Leave a comment