EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Alias

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Alias

PostgreSQL Alias

Introduction to PostgreSQL Alias

In simple terms, the ALIAS means temporarily giving another name to a table or a column. In order to give the temporary name for tables or columns, we generally use the PostgreSQL Aliases. The PostgreSQL Aliases are used to create a temporary name for a column or a table. 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.

Syntax

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 getting scanned multiple times to retrieve the data. The PostgreSQL Alias is used with the optional ‘alias’ keyword, but if it is provided, then 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 actually uses the ‘MT’ and not the ‘MyTable’.
  • Consider the following statements to understand how actually the PostgreSQL works for long table name:

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, the MAX(salary) is aliased as high_salary. So, 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

If we have spaces in the alias name, we should enclose it with quotes. 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 in order 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’, have a 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

This is a guide to PostgreSQL Alias. Here we discuss the introduction and how alias work in PostgreSQL and different examples and its code implementation. You may also look at the following articles to learn more –

  1. Joins in PostgreSQL
  2. PostgreSQL Triggers
  3. PostgreSQL GROUP BY
  4. PostgreSQL ORDER BY
  5. How to Works PostgreSQL LIMIT?
  6. Guide to PostgreSQL UNIQUE Constraint

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

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

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More