PostgreSQL Inner Join
This join is performed by giving an explicit join condition.PostgreSQL inner join is used to join data or records from two or more tables based on matching values ββin both tables.The inner join will create the resulting set until the condition is satisfied by combining all rows from both tables.The join is taken on the primary key and the foreign key of the two tables.The row will not appear in the output if it does not satisfy the given join condition.
This join is also called as an equi join since and = operators is used to give the join condition
Inner Joins Syntax :
The syntax of the Inner Joins statement is β
SELECT col_name,... FROM table_name1 t1 [INNER] JOIN table_name2 t2 ON t1.col_name = t2.col_name
PostgreSQL Inner Join Example :
Consider the Customer table with the following records -
ID | Name | Age | Address | Salary |
---|---|---|---|---|
1 | Aarav | 36 | Udaipur | 35000 |
2 | Vivaan | 33 | Mumbai | 30000 |
3 | Reyansh | 28 | Chennai | 40000 |
4 | Muhammad | 29 | Udaipur | 50000 |
5 | Sai | 27 | Mumbai | 27000 |
Consider the Order table with the following records -
OID | Date | CustomerID | Amount |
---|---|---|---|
102 | 2019-10-08 00:00:00 | 3 | 3000 |
100 | 2019-10-08 00:00:00 | 3 | 1500 |
101 | 2019-11-20 00:00:00 | 2 | 1800 |
103 | 2018-05-20 00:00:00 | 4 | 2500 |
TEST CASE 1 :- Let us join these two tables using the INNER JOIN as follows
lfcdb=# SELECT ID, Name, Amount, Date FROM Customer INNER JOIN Order ON Customer.ID = Order.CustomerID;
The result for the respective PostgreSQL query is as follows β
ID | Name | Amount | Date |
---|---|---|---|
3 | Reyansh | 3000 | 2019-10-08 00:00:00 |
3 | Reyansh | 1500 | 2019-10-08 00:00:00 |
2 | Vivaan | 1800 | 2019-11-20 00:00:00 |
4 | Muhammad | 2500 | 2018-05-20 00:00:00 |
Visit :
Discussion