Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
PostgreSQL - GRANT
Next article icon

PostgreSQL – Alias

Last Updated : 03 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PostgreSQL aliases are powerful tools that allow you to assign temporary names to tables or columns within your queries. These aliases only exist during the execution of the query, making your SQL code more readable and efficient.

What is a PostgreSQL Alias?

An alias in PostgreSQL is a temporary name given to a table or column. This can help simplify complex queries, improve readability, and make it easier to reference tables and columns.

For the sake of this article, we will be using the sample DVD rental database, which is explained here and can be downloaded by clicking on this link in our examples.

Column Aliases in PostgreSQL

Column aliases allow you to rename the columns in the result set of your query, making the output more meaningful.

Syntax

SELECT column_name AS alias_name FROM table;

or,

SELECT column_name alias_name FROM table;

You can also use column aliases with expressions.

SELECT expression alias_name FROM table;

The primary use of column alias is to make the output of a query more meaningful. The below example illustrates the use of column alias:

Example: Using Column Aliases

Here we will make a query to get full name of the customers from the “customer” table using column alias.

Query:

SELECT
first_name || ' ' || last_name AS full_name
FROM
customer
ORDER BY
full_name;

Output:

Explanation: This query concatenates the ‘first_name’ and ‘last_name’ columns into a single ‘full_name’ column for better readability.

Table Aliases in PostgreSQL

Table aliases provide a way to shorten table names in your queries, especially useful for tables with long names or when joining multiple tables.

Syntax

SELECT column_list FROM table_name AS alias_name;

or,

SELECT column_list FROM table_name alias_name;

Use Cases

There are multiple use-cases for table alias. Few of them are listed below:

  • It can be used to save some keystrokes and make your query more readable for tables with long names.
  • It can also be used when you query data from multiple tables that have the same column names.
  • It can be used to join a table with itself (ie, SELF JOIN).

Example: Using Table Aliases

Here we will be using table alias to avoid writing “address” for each column instead use a short form “add” as an alias to get the “district” and “postal_code” column from the “address” table of our database.

Query:

SELECT add.postal_code,
add.district
FROM address add;

Output:

Explanation: This query uses ‘add’ as an alias for the ‘address’ table, making the query shorter and easier to read.



Next Article
PostgreSQL - GRANT

R

RajuKumar19
Improve
Article Tags :
  • PostgreSQL
  • Python
  • postgreSQL-managing-table
Practice Tags :
  • python

Similar Reads

  • PostgreSQL - CAST
    The PostgreSQL CAST function provides an efficient way to convert data types in PostgreSQL, which is important when ensuring data is in the correct format for storage, calculations, or comparisons. In PostgreSQL, we can use CAST to transform data between various data types, such as converting string
    3 min read
  • PostgreSQL Clients
    The PostgreSQL client is a command-line tool used to interact with PostgreSQL databases. It allows users to manage databases, execute SQL queries, and perform various administrative tasks without needing a graphical interface. In this article we will cover the key features of the PostgreSQL client,
    4 min read
  • PostgreSQL - INSERT
    PostgreSQL INSERT statement is one of the fundamental SQL commands used to add new rows to a specified table within a PostgreSQL database. This command allows users to insert data efficiently, whether for a single record or multiple records at once. With the PostgreSQL INSERT INTO clause, we can spe
    5 min read
  • PostgreSQL List Users
    In PostgreSQL, managing users and their permissions is important for maintaining database security and organization. As a database administrator or developer, it's essential to understand how to list users and their roles within the PostgreSQL database system. Here user is an entity that can connect
    4 min read
  • PostgreSQL - GRANT
    In PostgreSQL, the GRANT statement is a powerful tool used to assign privileges to a role, allowing it to alter database objects like tables, views, functions, and more. Here we will learn about the syntax and application of the GRANT statement, with examples to illustrate its usage in PostgreSQL. S
    3 min read
  • PostgreSQL - WITH Clause
    The WITH clause in PostgreSQL, also known as a Common Table Expression (CTE), simplifies complex queries by breaking them into smaller, readable sections. It allows us to define temporary result sets that can be referenced later in our main query. This makes the PostgreSQL code easier to manage and
    4 min read
  • PostgreSQL ALTER DATABASE
    The PostgreSQL ALTER DATABASE statement is a powerful tool used for modifying an existing database.  The features of a database, once created can be changed using the ALTER DATABASE statement.  Let's explore the syntax of PostgreSQL ALTER DATABASE, the various actions that can be performed, and prac
    3 min read
  • PostgreSQL - Cursor
    In the area of database management, effective data retrieval is essential particularly when handling large datasets. PostgreSQL offers the functionality of a cursor which allows for incremental data retrieval from extensive result sets. By using PostgreSQL cursor syntax, developers can manage memory
    5 min read
  • PostgreSQL - COMMIT
    The COMMIT command in PostgreSQL is important for saving the changes made during a transaction. Without executing a COMMIT, all the data manipulation operations performed within the transaction will be lost once the session ends. It ensures that the changes made to the database are permanent and vis
    4 min read
  • PostgreSQL Exercises
    PostgreSQL is a powerful, open-source relational database system that supports complex queries, data types, and performance optimization features. This article provides PostgreSQL practice exercises with solutions, allowing learners to explore how joins, aggregations, triggers, and foreign keys work
    15+ 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