Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQL | Aliases
Next article icon

SQL | Aliases

Last Updated : 04 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, aliases are temporary names assigned to columns or tables for the duration of a query. They make the query more readable, especially when dealing with complex queries or large datasets. Aliases help simplify long column names, improve query clarity, and are particularly useful in queries involving multiple tables or aggregated data.

In this guide, we’ll learn SQL column aliases and SQL table aliases with a consistent example table and provide practical use cases to help you understand how and when to use them.

What Are SQL Aliases?

Aliases are the temporary names given to tables or columns for the purpose of a particular SQL query. It is used when the name of a column or table is used other than its original name, but the modified name is only temporary.

  • Aliases are created to make table or column names more readable.
  • The renaming is just a temporary change and the table name does not change in the original database.
  • Aliases are useful when table or column names are big or not very readable.
  • These are preferred when there is more than one table involved in a query.

There are two types of aliases in SQL:

  • Column Aliases: Temporary names for columns in the result set.
  • Table Aliases: Temporary names for tables used within a query.

We’ll use the following Customer table throughout the article to demonstrate all SQL alias concepts. This table contains customer information such as ID, name, country, age, and phone number.

CREATE TABLE Customer (     CustomerID INT PRIMARY KEY,     CustomerName VARCHAR(50),     LastName VARCHAR(50),     Country VARCHAR(50),     Age INT,     Phone VARCHAR(15) );  -- Inserting sample data into the Customer table INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone) VALUES  (1, 'Shubham', 'Thakur', 'India', 23, '9876543210'), (2, 'Aman', 'Chopra', 'Australia', 21, '9876543211'), (3, 'Naveen', 'Tulasi', 'Sri Lanka', 24, '9876543212'), (4, 'Aditya', 'Arpan', 'Austria', 21, '9876543213'), (5, 'Nishant', 'Jain', 'Spain', 22, '9876543214');

Output:

CustomerIDCustomerNameLastNameCountryAgePhone
1ShubhamThakurIndia239876543210
2AmanChopraAustralia219876543211
3NaveenTulasiSri Lanka249876543212
4AdityaArpanAustria219876543213
5NishantJainSpain229876543214

SQL Column Aliases

A column alias is used to rename a column in the output of a query. Column aliases are useful for making the result set more readable or when performing calculations or aggregations.

Syntax:

SELECT column_name AS alias_name

FROM table_name;

The following table explains the arguments in detail:

  • column_name: The column name can be defined as the column on which we are going to create an alias name.
  • alias_name: It can be defined as a temporary name that we are going to assign for the column or table. 
  • AS: It is optional. If you have not specified it, there is no effect on the query execution. 

Example 1: Column Alias for Renaming a Column

To fetch the CustomerID and rename it as id in the result set

SELECT CustomerID AS id FROM Customer;

Output:

id
1
2
3
4
5

SQL Table Aliases

A table alias is used when you want to give a table a temporary name for the duration of a query. Table aliases are especially helpful in JOIN operations to simplify queries, particularly when the same table is referenced multiple times (like in self-joins).

Example 2: Table Alias for Joining Tables

We want to join the Customer table with itself to find customers who have the same country and are aged 21. We'll use table aliases for each instance of the Customer table.

Query:

SELECT c1.CustomerName, c1.Country FROM Customer AS c1, Customer AS c2 WHERE c1.Age = c2.Age AND c1.Country = c2.Country;

Output:

CustomerNameCountry
ShubhamIndia
AmanAustralia
NaveenSri Lanka
AdityaAustria
NishantSpain

Here, c1 and c2 are aliases for two instances of the Customer table.

Combining Column and Table Aliases

We want to fetch customers who are aged 21 or older and rename the columns for better clarity. We'll use both table and column aliases.

Query:

SELECT c.CustomerName AS Name, c.Country AS Location
FROM Customer AS c
WHERE c.Age >= 21;

Output:

NameLocation
ShubhamIndia
AmanAustralia
NaveenSri Lanka
AdityaAustria
NishantSpain

Advantages of SQL Aliases

  • Readability: Aliases make long or complex table/column names more readable and concise.
  • Simplification: Aliases reduce the verbosity of SQL queries, especially in joins or complex calculations.
  • Clearer Results: Aliases help clarify what data is being returned, especially when performing aggregations or combining multiple tables.
  • Avoid Name Conflicts: Aliases prevent naming conflicts, especially when columns in multiple tables share the same name.

Conclusion

SQL aliases are an essential tool for simplifying complex queries, especially when dealing with multiple tables, aggregate functions, and subqueries. Whether you're renaming columns to make the output more understandable or using table aliases to handle multiple instances of the same table, aliases play a critical role in improving the clarity and maintainability of your SQL queries.


Next Article
SQL | Aliases

P

Pratik Agarwal
Improve
Article Tags :
  • Misc
  • SQL
  • Databases
Practice Tags :
  • Misc

Similar Reads

    MySQL Aliases
    MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main components of many websites and applications as the data is stored and exchanged over the web. Even all social networking websites mainly
    4 min read
    SQL Clauses
    Structured Query Language (SQL) is a powerful language used to manage and manipulate relational databases. One of the essential features of SQL is its clauses, which allow users to filter, sort, group, and limit data efficiently. SQL clauses simplify querying and enhance database performance by retr
    7 min read
    SQL Server ALIASES
    Aliases in SQL Server are the temporary names given to tables or columns to make it easy to read and maintain the data. Aliases help you to provide different names to columns and tables temporarily so that users can easily understand the data of the table and it does not change any data of the table
    3 min read
    SQL Literals
    There are four kinds of literal values supported in SQL. They are : Character string, Bit string, Exact numeric, and Approximate numeric. These are explained as following below. Character string : Character strings are written as a sequence of characters enveloped in single quotes. the only quote ch
    1 min read
    SQL - Show Databases
    In the dynamic scene of database management, having a good insight into the available databases for effective administration and development tasks. SHOW DATABASES command is designed to present all databases located on the server. The purpose of exploring the SQL SHOW DATABASES command is to give da
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences