DEV Community

Cover image for Introduction to PostgreSQL (Coding Style) Part-5
Vinay Kumar Talreja
Vinay Kumar Talreja

Posted on

Introduction to PostgreSQL (Coding Style) Part-5

This blog aims to assist you in understanding the concepts of PostgreSQL with complete coding as query language.

PostgreSQL SQL commands

Note: Make sure you have Postgres installed on your system to proceed to this tutorial.

PostgreSQL - ALIAS Syntax

  • In PostgreSQL, aliases can be used to temporarily rename tables or columns in queries.

  • Table aliases allow you to refer to a table by a different name within a specific query, but the actual table name remains unchanged in the database.

  • Column aliases are used to rename specific columns in query results, providing more meaningful or concise names for presentation without altering the underlying data.

Syntax Of Table Alias:

SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];

Syntax Of Column Alias:

SELECT column_name AS alias_name
FROM table_name
WHERE [condition];

Example:

Consider the following two tables, (1) COMPANY table is as follows −

id name age address salary
1 Paul 32 California 20000
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
4 Mark 25 Rich-Mond 65000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
7 James 24 Houston 10000

(2) DEPARTMENT as follows −

id dept emp_id
1 IT Billing 1
2 Engineering 2
3 Finance 7
4 Engineering 3
5 Finance 4
6 Engineering 5
7 Finance 6

TABLE ALIAS

Note: Usage of TABLE ALIAS where we use C and D as aliases for COMPANY and DEPARTMENT tables, respectively −

Query:

SELECT C.ID, C.NAME, C.AGE, D.DEPT
FROM COMPANY AS C, DEPARTMENT AS D
WHERE C.ID = D.EMP_ID;

Result:

id name age dept
1 Paul 32 IT Billing
2 Allen 25 Engineering
7 James 24 Finance
3 Teddy 23 Engineering
4 Mark 25 Finance
5 David 27 Engineering
6 Kim 22 Finance

Column ALIAS

Note: The usage of COLUMN ALIAS where COMPANY_ID is an alias of ID column and COMPANY_NAME is an alias of name column −

Query:

SELECT C.ID AS COMPANY_ID, C.NAME AS COMPANY_NAME, C.AGE, D.DEPT
FROM COMPANY AS C, DEPARTMENT AS D
WHERE C.ID = D.EMP_ID;

Result:

company_id company_name age dept
1 Paul 32 IT Billing
2 Allen 25 Engineering
7 James 24 Finance
3 Teddy 23 Engineering
4 Mark 25 Finance
5 David 27 Engineering
6 Kim 22 Finance

I hope this blog has helped you understand the concepts of PostgreSQL with complete coding as a query language.

Check out a summary of Part-6.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay