- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Aliasing in PostgreSQL?
Often, we have some very long table names, and writing the table name every time is troublesome. We can use aliasing to help us there, thanks to which, we will need to write the long table name only once.
The table aliases are generally written in the FROM part of the statement, or the JOIN part.
For example, consider that we have two tables, marks, and student_info, defined respectively below −
marks
name | roll_no | perc_marks |
---|---|---|
Aniket | 12 | 24 |
Siddhi | 45 | 65 |
Yash | 26 | 42 |
Isha | 56 | 87 |
student_info
name | roll_no | age | gender |
---|---|---|---|
Aniket | 12 | 26 | M |
Isha | 56 | 25 | F |
Siddhi | 45 | 23 | F |
Yash | 26 | 25 | M |
Now, if you want to see the name, roll_no, perc_marks, and age of the student in one query, your query will look like this −
SELECT marks.name, marks.roll_no, marks.perc_marks, student_info.age FROM marks LEFT JOIN student_info ON student_info.roll_no = marks.roll_no
The output will be
name | roll_no | perc_marks | age |
---|---|---|---|
Aniket | 12 | 24 | 26 |
Siddhi | 45 | 65 | 25 |
Yash | 26 | 42 | 23 |
Isha | 56 | 87 | 25 |
However, as you can see, this query is quite verbose. Now let us see how this query will look like, with aliasing.
SELECT a.name, a.roll_no, a.perc_marks, b.age FROM marks a LEFT JOIN student_info b ON b.roll_no = a.roll_no
As you can see, we had to write the name of the tables just once. Everywhere else, we used the table aliases, and it worked. The output will still be the same as above.
You can see that we have used the syntax ‘table_name alias’ in the above query. Another slightly more explanatory syntax is ‘table_name AS alias’. Thus, the above query can also be written as −
SELECT a.name, a.roll_no, a.perc_marks, b.age FROM marks AS a LEFT JOIN student_info AS b ON b.roll_no = a.roll_no
And it will give the same output.
name | roll_no | perc_marks | age |
---|---|---|---|
Aniket | 12 | 24 | 26 |
Siddhi | 45 | 65 | 25 |
Yash | 26 | 42 | 23 |
Isha | 56 | 87 | 25 |
- Related Articles
- Effects of Undersampling (Aliasing) and Anti-Aliasing Filter
- PHP Aliasing/Importing namespaces
- Why strict aliasing is required in C?
- CASE WHEN in PostgreSQL?
- How to create a table in PostgreSQL?
- Difference Between MySQL and PostgreSQL
- How to Kill queries in pgAdmin in PostgreSQL?
- Python Getting started with psycopg2-PostgreSQL
- How to Query a DB in pgAdmin in PostgreSQL?
- Simulating MySQL's ORDER BY FIELD() in PostgreSQL?
- How to define and query json columns in PostgreSQL?
- Create Primary Key on an existing table in PostgreSQL?
- How to insert a Python tuple in a PostgreSql database?
- How to get current timestamp and relative timestamps in PostgreSQL?
- How to add column to an existing table in PostgreSQL?
