Introduction to PostgreSQL (Coding Style) Part-5
Tags: postgres, apacheage, database, agenssql
Published: 2023-10-05 19:19:29

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](https://dev.to/vkt1271/introduction-to-postgresql-coding-style-part-6-3ihf).