EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL Aliases
 

SQL Aliases

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.

 

 

SQL Aliases

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Key Takeaways

  • 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:

Aliases

The tables used in the SQL query can be renamed using an alias.

Table aliases are written as follows:

Code:

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.

Code:

SELECT
 LastName,
PID
FROM Patients AS p
WHERE p.PID <=18;

The following information is returned once this query has been executed:

Table creation and value insertion

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:

Code:

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.

Code:

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');

Output:

Table of Patients

Example:

Code:

SELECT
  PID AS id,
  LastName AS lname,
  FirstName
FROM Patients
WHERE FirstName ="vishn";

Output:

SQL Aliases 3

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:

Code:

SELECT emp.id, custr.id, emp_name, emp_department
FROM employee1
INNER JOIN customerdemo
ON emp.id = customerdemo.emp_id

Output:

employee1 table

Frequently Asked Questions

Other FAQs are mentioned below:

Q1. What is SQL is an Alias?

Answer:

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?

Answer:

  • 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?

Answer:

  • 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.
Note: Using an alias can make your query easier to comprehend and more concise. By default, the output of aggregate functions is displayed in a column with no name. Using an alias is a fantastic method to give the output of your aggregate functions some context.

Conclusion

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.

Recommended Articles

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 –

  1. SQL BETWEEN
  2. SQL Wildcards
  3. SQL Schema
  4. sql_plus

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW