The SQL Aliases
The SQL Aliases are used to provide alternative names for columns or tables in a query’s result set in SQL, making the output more readable and user-friendly.
Aliases are especially useful when you need to rename columns or tables temporarily for the duration of a query.
Let’s have a look for examples of sql aliases.
Examples of SQL Aliases
- Column Aliases- You can assign an alias to a column in the SELECT statement to rename the column in the query’s result set.
SQL> SELECT FirstName || ‘ ‘ || LastName AS FullName
FROM Employees;
FirstName || ‘ ‘ || LastName concatenates the “FirstName” and “LastName” columns with a space in between.
- Table Aliases- You can assign an alias to a table in the FROM clause to make the SQL statement more concise, especially when working with complex queries involving multiple tables.
SQL> SELECT o.OrderID, od.ProductID, od.Quantity
FROM Orders AS o
INNER JOIN OrderDetails AS od ON o.OrderID = od.OrderID;
Orders AS o assigns the alias ”” to the ‘Orders’ table.
OrderDetails AS od assigns the alias ‘od’ to the ‘OrderDetails’ table.
- Alias for Aggregate Functions- You can assign an alias to the result of an aggregate function to provide a meaningful name for the calculated value.
SQL> SELECT AVG(Salary) AS AverageSalary
FROM Employees;
AVG(Salary) calculates the average salary.
AS AverageSalary assigns the alias ‘AverageSalary’ to the calculated average, providing a meaningful name for the result.