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 PostgreSQL Tutorial PostgreSQL Alias
 

PostgreSQL Alias

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 15, 2023

PostgreSQL Alias

 

 

Introduction to PostgreSQL Alias

In simple terms, ALIAS means temporarily giving another name to a table or a column. To give the temporary name for tables or columns, we generally use PostgreSQL Alias. The PostgreSQL Aliases are used to create a temporary column or table name. The existence of aliasing is limited to the PostgreSQL statement’s execution means the PostgreSQL aliases are used to rename a column or a table in a specific PostgreSQL query. Hence the actual table name or column name does not change in the database. We generally use the temporary names while performing self join on the table to create a temporary table.

Watch our Demo Courses and Videos

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

Syntax

We can use PostgreSQL Aliases to create a temporary name for columns, tables, or expressions. Let’s understand the syntax of Alias as below :

1. PostgreSQL Aliases for column

SELECT column [AS] alias_name
FROM table;

2. PostgreSQL Aliases for expression

SELECT expression [AS] alias_name
FROM table;

3. PostgreSQL Aliases for table

SELECT column
FROM table [AS] alias_name;

Explanation:

  • Column: The actual column name to which we want to specify an alias.
  • table_name: The actual table name to which we want to specify an alias.
  • expression: An expression to which we want to specify an alias.
  • AS: It is the Optional keyword. AS keyword will not affect the Alias in the PostgreSQL statement, even if it is defined or not. In PostgreSQL, defining AS keyword or not is a programmer’s choice.
  • alias_name: The temporary name for expressions, columns, or tables. The PostgreSQL Alias name can have spaces. But it is not a best practice to have an alias name with spaces in case of aliasing a table.

How does Alias Work in PostgreSQL?

The PostgreSQL Aliases are used to remove the ambiguity for self-joins. The self join means the same table is getting scanned multiple times to retrieve the data. The PostgreSQL Alias is used with the optional ‘alias’ keyword, but if provided, it hides the actual name of the columns or tables. If we have specified the Alias in the PostgreSQL statement, we need to define the column names, and the Alias defined, and its scope is limited to the same statement only.

Example:

  • Consider a statement ‘FROM MyTable AS MT’, which we are using with a SELECT statement; then it uses the ‘MT’ and not the ‘MyTable’.
  • Consider the following statements to understand how PostgreSQL works for long table names:
SELECT long_table_name.column
FROM long_table_name;

We can use the Alias for the long table name as follows:

SELECT ltn.column
FROM long_table_name ltn;

Here we have specified the alias ‘ltn’ for a table ‘long_table_name’.

Examples to Implement Alias in PostgreSQL

Let’s create a table of name ‘student’ and ‘teacher’ to understand the PostgreSQL alias examples in detail:

Example #1

Create a table of name ‘student.’

Code:

CREATE TABLE student(
rollno int PRIMARY KEY,
firstname VARCHAR (50) NOT NULL,
lastname VARCHAR (50) NOT NULL,
branch VARCHAR (50) NOT NULL,
result boolean,
joining_date DATE NOT NULL
);

Now, insert some data into thestudent’ table.

INSERT INTO student (rollno, firstname, lastname, branch, result, joining_date)
values
('101', 'Oliver','Jake', 'Civil', false, '06-01-2020'),
('102', 'Jack','Connor', 'Computer', false, '06-01-2020'),
('103', 'Harry','Callum', 'Civil', false, '06-01-2020'),
('104', 'Jacob','John', 'Computer', false, '06-01-2020'),
('105', 'Thomas','David', 'Civil', false, '06-01-2020');

Now, illustrate the data inserted into the ‘student’ table with the following SQL statement’s help.

select * from student;

PostgreSQL Alias-1.1

Example #2

Create a table of name ‘teacher.’

Code:

CREATE TABLE teacher (
teacher_id INT NOT NULL PRIMARY KEY,
firstname VARCHAR (50) NOT NULL,
lastname VARCHAR (50) NOT NULL,
branch VARCHAR (50) NOT null,
salary numeric
);

Now, insert some data into the ‘teacher’ table.

INSERT INTO teacher (teacher_id, firstname, lastname, branch,salary)
values
('1', 'Hugo','Smith', 'Computer',20000),
('2', 'Brayden','Johnson', 'Computer',30000),
('3', 'Ronan','Williams', 'Civil',35000),
('4', 'Antonio','Brown', 'Civil',40000),
('5', 'Marco','Davis', 'Civil',25000);

Now, illustrate the data inserted into the ‘teacher’ table with the following SQL statement’s help.

select * from teacher;

PostgreSQL Alias-1.2

1. PostgreSQL Aliases for column

We specify the alias names to make the column headers more readable in the final result set. Like whenever we use functions like MAX, we can alias the result of the MAX function to make it easier to read.

SELECT firstname, MAX(salary) AS high_salary
FROM teacher
GROUP BY firstname;

Illustrate the result of the above statement using the following snapshot.

PostgreSQL Alias-1.3

In the above example, we aliased MAX(salary) as ‘high_salary’. Therefore, the column header of the second column will be displayed as ‘high_salary’. In this example, we have not added any space in, so it does not need to add quotes around the given alias_name.

It is acceptable to add quotes around the alias_name in PostgreSQL Alias as follows:

SELECT firstname, MAX(salary) AS "high_salary"
FROM teacher
GROUP BY firstname;

Illustrate the result of the above statement using the following snapshot.

Output-1.4

We should enclose spaces with quotes if we have spaces in the alias name. Consider the following example.

SELECT firstname, MAX(salary) AS "high salary"
FROM teacher
GROUP BY firstname;

Illustrate the result of the above statement using the following snapshot.

Output-1.5

2. PostgreSQL Aliases for table

We generally use the Alias on the table if we want to abbreviate the name to the table to make the queries more readable and shorter, or in the case of SELF JOIN, where we use the same table multiple times. It is acceptable to define aliases for the tables you want to give a temporary name and not for all tables.

Let’s consider the following example to understand the table alias.

SELECT s.firstname, s.branch, teacher.firstname
FROM student s
INNER JOIN teacher
ON s.branch = teacher.branch
ORDER BY s.rollno asc;

Illustrate the result of the above statement using the following snapshot.

Output-1.6

In the above statement for the ‘student’ table, we have created Alias s. So in this statement, we can use ‘s’ instead of the student table as it refers to the ‘student’ table.

Now we will add an alias for the ‘teacher’ table as ‘t’; look at the following example.

SELECT s.firstname, s.branch, t.firstname
FROM student s
INNER JOIN teacher t
ON s.branch = t.branch
ORDER BY s.rollno asc;

Illustrate the result of the above statement using the following snapshot.

Output-1.7

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Alias” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Joins in PostgreSQL
  2. PostgreSQL Triggers
  3. How to Works PostgreSQL LIMIT?
  4. Guide to PostgreSQL UNIQUE Constraint

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