SlideShare a Scribd company logo
1
Joins between Tables
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
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
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
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
6
Types of PostgreSQL JOIN ….
7
Types of PostgreSQL JOIN …
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
Types of PostgreSQL JOIN
• Finally, we have the following list of records available in DEPARTMENT table −
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
1. The CROSS JOIN : Example 1
• The above given query will produce the following result −
12
1. The CROSS JOIN : Example 2
• Sample table: foods
• Sample table: company
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;
14
1. The CROSS JOIN : Example 2 …
• Output:
15
1. The CROSS JOIN : Example 2 …
• More presentation of the said output:
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
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
2. The INNER JOIN : Example 2
• Sample table: foods
• Sample table: company
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;
20
2. The INNER JOIN : Example 2 …
• Output:
21
2. The INNER JOIN : Example 2 …
• Pictorial Presentation:
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
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
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
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
3. The LEFT OUTER JOIN : Example 2
• Sample table: foods
• Sample table: company
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;
28
3. The LEFT OUTER JOIN : Example 2 …
• Output:
29
3. The LEFT OUTER JOIN : Example 2 …
• Pictorial Presentation of the above example:
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
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
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
4. The RIGHT OUTER JOIN : Example 2
• Sample table: foods
• Sample table: company
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;
35
4. The RIGHT OUTER JOIN : Example 2 …
• Output:
36
4. The RIGHT OUTER JOIN : Example 2 …
• Pictorial Presentation of the above example:
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
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
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
5. The FULL OUTER JOIN : Example 2
• Sample table: foods
• Sample table: company
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
5. The FULL OUTER JOIN : Example 2 …
• Pictorial Presentation of the above example:
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;
44
5. The FULL OUTER JOIN : Example 2 …
• Output:
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

More Related Content

PPSX
Join query
PPTX
Joins and Views.pptx
PPTX
SQL Joins and View.pptx
PPTX
MYSQL join
PDF
Lesson 6 - Relational Algebra.pdf
PPTX
REC-UNIT-2-DATABASEMANAGEMENTSYSTEMS.pptx
PDF
Advance database system(part 8)
PPTX
Join in SQL - Inner, Self, Outer Join
Join query
Joins and Views.pptx
SQL Joins and View.pptx
MYSQL join
Lesson 6 - Relational Algebra.pdf
REC-UNIT-2-DATABASEMANAGEMENTSYSTEMS.pptx
Advance database system(part 8)
Join in SQL - Inner, Self, Outer Join

Similar to Joins (A JOIN clause is used to combine rows from two or more tables, based on a related column between them.).pptx (20)

PPTX
The Relational Database Model 2 univprsty
PDF
SQL JOINS
PPTX
PDF
Assignment 4
PDF
DBMS Nested & Sub Queries Set operations
PPTX
MS Excel Learning for PPC Google AdWords Training Course
PPT
Displaying data from multiple tables
PPTX
SQL JOIN.pptx
PPT
Types Of Join In Sql Server - Join With Example In Sql Server
PPTX
PPTX
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
PPTX
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
PPTX
SQL Server Learning Drive
PDF
Microsoft Excel 365 Formulas BarCharts QuickStudy 1st Edition Curtis Frye
PPTX
Joins in SQL
PPTX
types of SQL Joins
PPTX
Sql joins final
PPTX
MS SQLSERVER:Joining Databases
PPTX
MS Sql Server: Joining Databases
The Relational Database Model 2 univprsty
SQL JOINS
Assignment 4
DBMS Nested & Sub Queries Set operations
MS Excel Learning for PPC Google AdWords Training Course
Displaying data from multiple tables
SQL JOIN.pptx
Types Of Join In Sql Server - Join With Example In Sql Server
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
SQL Server Learning Drive
Microsoft Excel 365 Formulas BarCharts QuickStudy 1st Edition Curtis Frye
Joins in SQL
types of SQL Joins
Sql joins final
MS SQLSERVER:Joining Databases
MS Sql Server: Joining Databases
Ad

More from BINJAD1 (20)

PPTX
PostgreSQL (PostgreSQL is a versatile, open-source object-relational database...
PPTX
3D-Object Representation in Computer Graphics.pptx
PPTX
Visible Surface Detection Methods in Computer Graphics.pptx
PDF
Number Systems (These are ways of representing numbers using symbols and rule...
PDF
Computer Logical Organization(It refers to how its functional units are arran...
PDF
Logic Gates(Logic gates are fundamental building blocks of digital circuits).pdf
PDF
Digital Concepts (Digital electronics is a branch of electronics).pdf
PDF
sequential logic circuits- Latch & Flip Flop.pdf
PDF
Object Oriented Programming with Java Basic Syntax.pdf
PDF
AWT (Abstract Window Toolkit) Controls.pdf
PDF
Introduction to Microsoft Access (MS Access).pdf
PPTX
Database Administration (Database Administrator (DBA) is a professional respo...
PPTX
Database Administration (Database Administrator (DBA) is a professional respo...
PPTX
Database (DB- A database is an electronically stored, systematic collection o...
PPTX
Pixel- A pixel, short for "picture element.pptx
PPT
Introduction to Computer Graphics or CG.ppt
PPT
2D-Transformations-Transformations are the operations applied to geometrical ...
PPTX
2D Viewing- the window by setting a two-dimensional viewing co-ordinate syst...
PPTX
The internet and WWW-he terms World Wide Web (WWW) and the Internet
PPTX
Structured Query Language (SQL)- standard Database language
PostgreSQL (PostgreSQL is a versatile, open-source object-relational database...
3D-Object Representation in Computer Graphics.pptx
Visible Surface Detection Methods in Computer Graphics.pptx
Number Systems (These are ways of representing numbers using symbols and rule...
Computer Logical Organization(It refers to how its functional units are arran...
Logic Gates(Logic gates are fundamental building blocks of digital circuits).pdf
Digital Concepts (Digital electronics is a branch of electronics).pdf
sequential logic circuits- Latch & Flip Flop.pdf
Object Oriented Programming with Java Basic Syntax.pdf
AWT (Abstract Window Toolkit) Controls.pdf
Introduction to Microsoft Access (MS Access).pdf
Database Administration (Database Administrator (DBA) is a professional respo...
Database Administration (Database Administrator (DBA) is a professional respo...
Database (DB- A database is an electronically stored, systematic collection o...
Pixel- A pixel, short for "picture element.pptx
Introduction to Computer Graphics or CG.ppt
2D-Transformations-Transformations are the operations applied to geometrical ...
2D Viewing- the window by setting a two-dimensional viewing co-ordinate syst...
The internet and WWW-he terms World Wide Web (WWW) and the Internet
Structured Query Language (SQL)- standard Database language
Ad

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Programs and apps: productivity, graphics, security and other tools
The Rise and Fall of 3GPP – Time for a Sabbatical?
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MIND Revenue Release Quarter 2 2025 Press Release
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
Machine learning based COVID-19 study performance prediction
Spectroscopy.pptx food analysis technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
20250228 LYD VKU AI Blended-Learning.pptx

Joins (A JOIN clause is used to combine rows from two or more tables, based on a related column between them.).pptx

  • 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;
  • 14. 14 1. The CROSS JOIN : Example 2 … • Output:
  • 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;
  • 20. 20 2. The INNER JOIN : Example 2 … • Output:
  • 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;
  • 28. 28 3. The LEFT OUTER JOIN : Example 2 … • Output:
  • 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;
  • 35. 35 4. The RIGHT OUTER JOIN : Example 2 … • Output:
  • 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;
  • 44. 44 5. The FULL OUTER JOIN : Example 2 … • Output:
  • 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