Updated April 8, 2023
Introduction to SQL Aliases
Aliasing temporarily renames a table or column by assigning it a new name. Table aliases are employed to rename a table in a particular SQL statement. The databases don’t alter the table’s name; the renaming is only a temporary change. For the sake of a specific SQL query, the fields of a table are renamed using column aliases.
- An alias is a different name for a specific item, such as a table, view, sequence, or another alias. Anywhere an object may be directly referred to, it can be used to refer to that object.
- Using an alias does not require permission.
- Aliases are helpful when the table’s name is too long or irrelevant.
What are SQL Aliases?
A table or a field within a table can have a temporary name by using SQL aliases. Aliases were frequently used to improve the readability of column names. Column and table aliases are the two types of aliases that SQL provides. Column alias enhances the significance of query results. Table aliases make it easier to avoid using the complete table name, which speeds up the execution of queries. Additionally, the table alias prevents ambiguous table name errors when a table is referenced more than once in a query, such as in an inner join, left join, or subquery.
How to Use SQL Aliases?
The query statement in which the alias is used is the only restriction on its availability. Names may grow lengthy and complex as requests become more specific. We can use aliases to give the query’s objects new names to make things more understandable.
SQL Aliases Column and Tables
Column aliases and table aliases are the two types of aliases that MySQL provides.
1. Column Alias in MySQL
Often, column names are so complex that it’s hard to interpret the results of the request. One can use a column alias to give a column a meaningful name. The AS keyword is used in conjunction with the alias to assign an alias to a column.
The following syntax demonstrates the use of the column alias:
The tables used in the SQL query can be renamed using an alias.
Table aliases are written as follows:
SELECT col_1, col_2, … FROM table_name AS alias_name;
In MySQL, we can use the column alias in the ORDER BY, GROUP BY, and HAVING clauses to refer to the column.
Let’s examine how alias functions: The Table creation and value insertion are given below.
SELECT LastName, PID FROM Patients AS p WHERE p.PID <=18;
The following information is returned once this query has been executed:
The table alias must be used in place of the actual table name if the table name appears in the WHERE clause before a column name. Additionally, one needs to use the alias while specifying the table in the SELECT list (e.g., c.lname). Yet again, the AS keyword is not required. Without that, the request will function as intended.
Alias Column Name:
SELECT column_name AS alias_name FROM table_name;
Now, for example, let’s create a Table of Patients as below and also insert some records for manipulation.
CREATE TABLE Patients ( PID int, LastName varchar(25), FirstName varchar(25), Address varchar(20), City varchar(15) ); INSERT INTO Patients (PID, LastName, FirstName,Address,city) VALUES (12,'Cavin', 'Tom B. Erichsen', 'Sweden 21','Norway'); INSERT INTO Patients (PID, LastName, FirstName,Address,city) VALUES (18,'Anu', 'vishn', '34 usa', 'canada');
SELECT PID AS id, LastName AS lname, FirstName FROM Patients WHERE FirstName ="vishn";
2. Table Alias
Table aliases are typically used to retrieve data from multiple tables and link them together using field relations.
- To display the entire record or specific columns from a table, utilize table aliases in SELECT lists and the FROM clause.
- WHERE, GROUP BY, HAVING, and ORDER BY clauses all support the use of table aliases.
- When many tables are needed for data, those tables must be joined by qualifying the columns with the table name or table alias.
SQL Aliases Syntax:
SELECT a1.col_name (AS) new_col_name, a2.col_name (AS) other_new_col_name, ... FROM table1 (AS) a1 JOIN table2 (AS) a2 ON a1.col_name = a2.col_name
The table or column name refers to the column’s name in the table to which the alternative name must be assigned.
Let’s take an employee1 table and use the JOIN Statement:
SELECT emp.id, custr.id, emp_name, emp_department FROM employee1 INNER JOIN customerdemo ON emp.id = customerdemo.emp_id
Frequently Asked Questions
Other FAQs are mentioned below:
Q1. What is SQL is an Alias?
An alias is a temporary access name for a database, column, or query result. We construct a temporary alias that lasts for the query execution. The alias is automatically gone once the query has finished running.
Q2. Why is an alias needed in SQL?
- To improve the readability of the query result.
- To improve the user’s comprehension by identifying appropriate names.
Q3. What are some of the most common uses for an alias?
- Using an alias makes the result simple to interpret when searching multiple tables.
- Using an alias improves the output’s readability when our query involves complicated functions.
- If the column names are still not readable, it helps to make them so.
This article has shown the importance of SQL alias, and when to use SQL in a query has been thoroughly covered. We know every scenario and combination used when utilizing an alias in SQL. Furthermore, we could interpret the fundamental idea of column aliases by using a variety of sample queries on our demo database.
This is a guide to SQL Aliases. Here we discuss the introduction and how to use SQL aliases. Column & tables and FAQ, respectively. You may also have a look at the following articles to learn more –