2. 2
PostgreSQL - JOINS
• So far, you have learned how to select data from a table, choosing which columns
and rows you want, and how to sort the result set in a particular order.
• Thus far, our queries have only accessed one table at a time.
• Queries can access multiple tables at once, or access the same table in such a
way that multiple rows of the table are being processed at the same time.
• A query that accesses multiple rows of the same or different tables at one time is
called a join query.
3. 3
PostgreSQL – JOINS …
• As an example, say you wish to list all the weather records together with the location of the
associated city.
• To do that, we need to compare the city column of each row of the weather table with the name
column of all rows in the cities table, and select the pairs of rows where these values match.
• Note:
• This is only a conceptual model.
• The join is usually performed in a more efficient manner than actually comparing each possible
pair of rows, but this is invisible to the user.
4. 4
What are PostgreSQL Joins?
• PostgreSQL JOINs are used for retrieving data from more than one tables.
• With JOINs, it is possible for us to combine the SELECT and JOIN statements into a single
statement.
• A JOIN condition is added to the statement, and all rows that meet the conditions are
returned.
• The values from different tables are combined based on common columns.
• The common column mostly is a primary key in the first table and a foreign key of the second
table.
5. 5
Types of PostgreSQL JOIN
• The PostgreSQL Joins clause is used to combine records from two or more tables in a database.
• A JOIN is a means for combining fields from two tables by using values common to each.
• Join Types in PostgreSQL are −
1) The CROSS JOIN
2) The INNER JOIN
3) The OUTER JOIN
I. The LEFT OUTER JOIN
II. The RIGHT OUTER JOIN
III. The FULL OUTER JOIN
8. 8
Types of PostgreSQL JOIN …
• Before we proceed, let us consider two tables, COMPANY and DEPARTMENT.
• We already have seen INSERT statements to populate COMPANY table.
• So just let us assume the list of records available in COMPANY table −
9. 9
Types of PostgreSQL JOIN
• Finally, we have the following list of records available in DEPARTMENT table −
10. 10
1. The CROSS JOIN
• A CROSS JOIN matches every row of the first table with every row of the second table.
• If the input tables have x and y columns, respectively, the resulting table will have x+y columns.
• Because CROSS JOINs have the potential to generate extremely large tables, care must be taken to use them
only when appropriate.
• The following is the syntax of CROSS JOIN −
• Based on the above tables, we can write a CROSS JOIN as follows −
SELECT ... FROM table1 CROSS JOIN table2 ...
11. 11
1. The CROSS JOIN : Example 1
• The above given query will produce the following result −
12. 12
1. The CROSS JOIN : Example 2
• Sample table: foods
• Sample table: company
13. 13
1. The CROSS JOIN : Example 2 …
• To get item name and item unit columns from foods table and company name, company city columns from company
table, after a CROSS JOINING with these mentioned tables, the following SQL statement can be used:
• How cross joining happened into two tables
SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city
FROM foods
CROSS JOIN company;
15. 15
1. The CROSS JOIN : Example 2 …
• More presentation of the said output:
16. 16
2. The INNER JOIN
• A INNER JOIN creates a new result table by combining column values of two tables (table1
and table2) based upon the join-predicate.
• The query compares each row of table1 with each row of table2 to find all pairs of rows,
which satisfy the join-predicate.
• When the join-predicate is satisfied, column values for each matched pair of rows of table1
and table2 are combined into a result row.
• An INNER JOIN is the most common type of join and is the default type of join.
17. 17
2. The INNER JOIN : Example 1
• The following is the syntax of INNER JOIN −
• Based on the above tables, we can write an INNER JOIN as follows −
• The above given query will produce the following result −
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_filed = table2.common_field;
SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN
DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
18. 18
2. The INNER JOIN : Example 2
• Sample table: foods
• Sample table: company
19. 19
2. The INNER JOIN : Example 2 …
• To join item name, item unit columns from foods table and company name, company city
columns from company table, with the following condition -
• company_id of foods and company table must be same,
• the following SQL statement can be used :
• SQL Code: SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city
FROM foods
JOIN company
ON foods.company_id =company.company_id;
21. 21
2. The INNER JOIN : Example 2 …
• Pictorial Presentation:
22. 22
2. The INNER JOIN : Example 3
• Here is two table tableX and tableY and they have no duplicate rows in each.
• In tableX the values ( A,B) are unique and in tableY the values (E,F) are unique, but the
values (C and D) are common in both the tables.
23. 23
2. The INNER JOIN : Example 3 …
• Here is INNER JOIN
Output:
• Here only the matching of both tableX and tableY have appeared in the result set.
SELECT *
FROM tableX
INNER JOIN tableY on tableX.X = tableY.Y;
24. 24
3. The LEFT OUTER JOIN
• The OUTER JOIN is an extension of the INNER JOIN.
• SQL standard defines three types of OUTER JOINs: LEFT, RIGHT, and FULL and PostgreSQL supports
all of these.
• In case of LEFT OUTER JOIN, an inner join is performed first.
• Then, for each row in table T1 that does not satisfy the join condition with any row in table
T2, a joined row is added with null values in columns of T2.
• Thus, the joined table always has at least one row for each row in T1.
25. 25
3. The LEFT OUTER JOIN : Example 1
• The following is the syntax of LEFT OUTER JOIN −
• Based on the above tables, we can write an inner join as follows −
• The above given query will produce the following result −
SELECT ... FROM table1 LEFT OUTER JOIN table2 ON conditional_expression ...
SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
26. 26
3. The LEFT OUTER JOIN : Example 2
• Sample table: foods
• Sample table: company
27. 27
3. The LEFT OUTER JOIN : Example 2 …
• SQL Code:
• Explanation:
• This SQL statement would return all rows from the company table and only those rows from
the foods table where the joined fields are equal and if the ON clause matches no records in
the 'foods' table, the join will still return rows, but the NULL in each column of the right
table.
SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM company
LEFT JOIN foods
ON company.company_id = foods.company_id;
29. 29
3. The LEFT OUTER JOIN : Example 2 …
• Pictorial Presentation of the above example:
30. 30
3. The LEFT OUTER JOIN : Example 3
• Here is LEFT OUTER JOIN
• Output:
• Here, all the rows from tableX that is left side of JOIN clause and all the rows
with NULL values for unmatched columns from tableY that is the right side of
JOIN clause have appeared.
SELECT *
FROM tableX
LEFT OUTER JOIN tableY ON
tableX.X= tableY.Y
31. 31
4. The RIGHT OUTER JOIN
• First, an inner join is performed.
• Then, for each row in table T2 that does not satisfy the join condition with any row in table
T1, a joined row is added with null values in columns of T1.
• This is the converse of a left join; the result table will always have a row for each row in T2.
• The following is the syntax of RIGHT OUTER JOIN −
SELECT ... FROM table1 RIGHT OUTER JOIN table2 ON conditional_expression ...
32. 32
4. The RIGHT OUTER JOIN : Example 1
• Based on the above tables, we can write an inner join as follows −
• The above given query will produce the following result −
SELECT EMP_ID, NAME, DEPT FROM COMPANY RIGHT OUTER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
33. 33
4. The RIGHT OUTER JOIN : Example 2
• Sample table: foods
• Sample table: company
34. 34
4. The RIGHT OUTER JOIN : Example 2 …
• SQL Code:
• Explanation:
• This SQL statement would return all rows from the foods table and only those rows from the
company table where the joined fields are equal and if the ON clause matches no records in
the company table, the join will still return rows, but the NULL in each column of company
table.
SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM company
RIGHT JOIN foods
ON company.company_id = foods.company_id;
36. 36
4. The RIGHT OUTER JOIN : Example 2 …
• Pictorial Presentation of the above example:
37. 37
4. The RIGHT OUTER JOIN : Example 3
• Here is RIGHT OUTER JOIN
• Output:
• Here, all the rows from tableY that is the right side of JOIN clause and all the rows with NULL values for
unmatched columns from tableX that is left side of JOIN clause have appeared.
SELECT * FROM tableX
RIGHT OUTER JOIN tableY ON
tableX.X= tableY.Y
38. 38
5. The FULL OUTER JOIN
• In SQL the FULL OUTER JOIN combines the results of both left and right outer joins and returns all (matched
or unmatched) rows from the tables on both sides of the join clause.
• First, an inner join is performed.
• Then, for each row in table T1 that does not satisfy the join condition with any row in table T2, a joined row is
added with null values in columns of T2.
• In addition, for each row of T2 that does not satisfy the join condition with any row in T1, a joined row with null
values in the columns of T1 is added.
• The following is the syntax of FULL OUTER JOIN −
SELECT ... FROM table1 FULL OUTER JOIN table2 ON conditional_expression ...
39. 39
5. The FULL OUTER JOIN : Example 1
• Based on the above tables, we can write an inner join as follows −
• The above given query will produce the following result −
SELECT EMP_ID, NAME, DEPT FROM COMPANY FULL OUTER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
40. 40
5. The FULL OUTER JOIN : Example 2
• Sample table: foods
• Sample table: company
41. 41
5. The FULL OUTER JOIN : Example 2 …
• As we know the FULL OUTER JOIN is the combination of the results of both LEFT OUTER JOIN and RIGHT
OUTER JOIN, so, here we are going to describe how FULL OUTER JOIN perform internally.
• SQL Code & o/p
(Both LEFT & RIGHT
OUTER JOINS):
42. 42
5. The FULL OUTER JOIN : Example 2 …
• Pictorial Presentation of the above example:
43. 43
5. The FULL OUTER JOIN : Example 2 …
• SQL Code:
• Output:
SELECT a.company_id AS "a.ComID",
a.company_name AS "C_Name",
b.company_id AS "b.ComID",
b.item_name AS "I_Name"
FROM company a
FULL OUTER JOIN foods b
ON a.company_id = b.company_id;
45. 45
5. The FULL OUTER JOIN : Example 3
• Here is FULL OUTER JOIN
• Output:
• Here, all the matching rows from tableX and tableY and all the unmatched rows with NULL values for
both the tables have appeared.
SELECT *
FROM tableX
FULL OUTER JOIN tableY ON
tableX.X= tableY.Y