PostgreSQL Joins
Joins:
PostgreSQL JOINS are used to retrieve data from multiple tables. A PostgreSQL JOIN is performed whenever two or more tables are joined in a SQL statement.Types:
1.Basic Join
2.Inner Join
1.Equal Inner Join
2.Non-equal Inner Join
3.Outer Join
1.Left Outer Join
2.Right Outer Join
3.Full Outer Join
4.Cross Join
2.Inner Join
1.Equal Inner Join
2.Non-equal Inner Join
3.Outer Join
1.Left Outer Join
2.Right Outer Join
3.Full Outer Join
4.Cross Join
Examples:
Sample Tables:pearl=# select * from muthu;
id | dob | place
----+------------+---------
1 | 1992-04-16 | theni
4 | 1991-07-22 | madurai
5 | 1991-07-22 | covai
(3 rows)
pearl=# select * from siva;
id | name | age | salary
----+----------+-----+--------
1 | muthu | 21 | 2000
2 | siva | 26 | 4000
3 | thanigai | 27 | 5000
(3 rows)
Basic Join:
pearl=# select a.id,a.name,b.dob from siva a,muthu b where a.id=b.id;id | name | dob
----+-------+------------
1 | muthu | 1992-04-16
(1 row)
Inner Join:
Equal Inner Join:
pearl=# select * from siva a inner join muthu b on a.id=b.id;id | name | age | salary | id | dob | place
----+-------+-----+--------+----+------------+-------
1 | muthu | 21 | 2000 | 1 | 1992-04-16 | theni
(1 row)
Non-equal Inner Join:
pearl=# select * from siva a inner join muthu b on a.id!=b.id;id | name | age | salary | id | dob | place
----+----------+-----+--------+----+------------+---------
2 | siva | 26 | 4000 | 1 | 1992-04-16 | theni
3 | thanigai | 27 | 5000 | 1 | 1992-04-16 | theni
1 | muthu | 21 | 2000 | 4 | 1991-07-22 | madurai
2 | siva | 26 | 4000 | 4 | 1991-07-22 | madurai
3 | thanigai | 27 | 5000 | 4 | 1991-07-22 | madurai
1 | muthu | 21 | 2000 | 5 | 1991-07-22 | covai
2 | siva | 26 | 4000 | 5 | 1991-07-22 | covai
3 | thanigai | 27 | 5000 | 5 | 1991-07-22 | covai
(8 rows)
Outer Join:
Left Outer Join:
pearl=# select a.name,b.place from siva a left join muthu b on a.id=b.id;name | place
----------+-------
muthu | theni
siva |
thanigai |
(3 rows)
Right Outer Join:
pearl=# select a.name,b.place from siva a right join muthu b on a.id=b.id;name | place
-------+---------
muthu | theni
| madurai
| covai
(3 rows)
Full Outer Join:
pearl=# select a.name,b.place from siva a full join muthu b on a.id=b.id;name | place
----------+---------
muthu | theni
siva |
thanigai |
| madurai
| covai
(5 rows)
Cross Join:
pearl=# select a.id,b.id from siva a cross join muthu b;id | id
----+----
1 | 1
2 | 1
3 | 1
1 | 4
2 | 4
3 | 4
1 | 5
2 | 5
3 | 5
(9 rows)
Comments
Post a Comment