SlideShare a Scribd company logo
Chapter - 4

Displaying Data
from Multiple Tables
Joins
Obtaining Data from Multiple Tables
Cartesian Products


When a join condition is invalid or omitted completely, the result is a
Cartesian product, in which all combinations of rows are displayed.



All rows in the first table are joined to all rows in the second table.



A Cartesian product tends to generate a large number of rows, and the
result is rarely useful.



You should always include a valid join condition in a WHERE clause, unless
you have a specific need to combine all rows from all tables.



Cartesian products are useful for some tests when you need to generate a
large number of rows to simulate a reasonable amount of data.
Join sql
Types of Joins
Joining Tables Using Oracle Syntax


Use a join to query data from more than one table.



Write the join condition in the WHERE clause.



Prefix the column name with the table name when the same column
name appears in more than one table.
Defining Joins
Guidelines






When data from more than one
table in the database is required, a
join condition is used.
Rows in one table can be joined to
rows in another table according to
common values existing in
corresponding columns, that is,
usually primary and foreign key
columns.
To display data from two or more
related tables, write a simple join
condition in the WHERE clause.



When writing a SELECT statement that
joins tables, precede the column name with
the table name for clarity and to enhance
database access.



If the same column name appears in more
than one table, the column name must be
prefixed with the table name.



To join n tables together, you need a
minimum of n-1 join conditions.



For example, to join four tables, a minimum
of three joins is required.



This rule may not apply if your table has a
concatenated primary key, in which case
more than one column is required to
uniquely identify each row.
What is an Equi-join?
Equijoins


To determine an employee’s department name, you compare the
value in the DEPARTMENT_ID column in the EMPLOYEES table
with the DEPARTMENT_ID values in the DEPARTMENTS table.



The relationship between the EMPLOYEES and DEPARTMENTS
tables is an equijoin—that is, values in the DEPARTMENT_ID
column on both tables must be equal.



Frequently, this type of join involves primary and foreign key
complements.



Note: Equijoins are also called simple joins or inner joins.
Decision Matrix


Explain the use of a decision matrix for simplifying writing joins.



For example, if you want to display the name and department number of all the
employees who are in the same department as Goyal, you can start by making the
following decision tree:



Now the SQL statement can be easily formulated by looking at the decision matrix.



The first column gives the column list in the SELECT statement, the second column
gives the tables for the FROM clause, and the third column gives the condition for the
WHERE clause.
Qualifying Ambiguous Column Names
Guidelines
Table aliases can be max. up to 30
characters in length, but shorter is
better.





Use table prefixes to qualify column
names that are in multiple tables.






If a table alias is used for a particular
table name in the FROM clause, then
that table alias must be substituted for
the table name throughout the
SELECT statement.



Table aliases should be meaningful.



The table alias is valid only for the
current SELECT statement.

Distinguish columns that have identical
names but reside in different tables by
using column aliases.

Using Table Aliases


Simplify queries by using table aliases.



Improve performance by using table
prefixes.
Joining More than Two Tables
Non-Equijoins
A non-equijoin is a join condition containing something other than an equality operator.
Outer Joins
Outer Joins Syntax


You use an outer join to also see rows that do not meet the join condition.



The Outer join operator is the plus sign (+).

(+) is placed on the “side” of the join that is deficient in information.
Self Joins


Sometimes you need to join a table to itself.
Joining Tables Using SQL: 1999 Syntax











table1.column
CROSS JOIN
NATURAL JOIN
JOIN table
USING
JOIN table ON
table1.column_name
= table2.column_name
LEFT/RIGHT/FULL OUTER

Denotes the table and column from which data is retrieved
Returns a Cartesian product from the two tables
Joins two tables based on the same column name
column_name Performs an equijoin based on the column name
Performs an equijoin based on the condition in the ON clause
Creating Cross Joins


The CROSS JOIN clause produces the cross product of two tables.



This is the same as a Cartesian product between the two tables.
Creating Natural Joins


The NATURAL JOIN clause is based on all columns in the two tables that have the same name.



It selects rows from the two tables that have equal values in all matched columns.



If the columns having the same names have different data types, an error is returned.
Creating Joins with the USING Clause


If several columns have the same names but the data types do not match, the NATURAL JOIN
clause can be modified with the USING clause to specify the columns that should be used for an
equi-join.



Use the USING clause to match only one column when more than one column matches.



Do not use a table name or alias in the referenced columns.



The NATURAL JOIN and USING clauses are mutually exclusive.
Creating Joins with the ON Clause


The join condition for the natural join is basically an equi-join of all columns with the same name.



To specify arbitrary conditions or specify columns to join, the ON clause is used.



The join condition is separated from other search conditions.



The ON clause makes code easy to understand.
Creating Three-Way Joins with the ON Clause
INNER Joins Versus OUTER Joins


In SQL: 1999, the join of two tables returning only matched rows is an
inner join.



A join between two tables that returns the results of the inner join as well as
unmatched rows left (or right) tables is a left (or right) outer join.



A join between two tables that returns the results of an inner join as well as
the results of a left and right join is a full outer join.
Joins - Comparing SQL: 1999 to Oracle Syntax
LEFT OUTER JOIN


This query retrieves all rows in the EMPLOYEES table, which is the left table even if
there is no match in the DEPARTMENTS table.

This query was completed in earlier releases as follows:
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE d.department_id (+) = e.department_id;
RIGHT OUTER JOIN


This query retrieves all rows in the DEPARTMENTS table, which is the right table
even if there is no match in the EMPLOYEES table.

This query was completed in earlier releases as follows:
SELECT e.last_name,e.department_id,d.department_name
FROM employees e, departments d
WHERE d.department_id = e.department_id (+);
FULL OUTER JOIN


This query retrieves all rows in the EMPLOYEES table, even if there is no match in the
DEPARTMENTS table. It also retrieves all rows in the DEPARTMENTS table, even if there is no
match in the EMPLOYEES table.



It was not possible to complete this in earlier releases using outer joins. However, you could
accomplish the same results using the UNION operator.

SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id (+) = d.department_id
UNION
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id (+);
Additional Conditions
Summary


There are multiple ways to join tables.

Types of Joins








Equijoins
Non-equijoins
Outer joins
Self joins
Cross joins
Natural joins
Full or outer joins

Cartesian Products
A Cartesian product results in all combinations of rows displayed. This is done by either omitting the WHERE
clause or specifying the CROSS JOIN clause.
Table Aliases



Table aliases speed up database access.
Table aliases can help to keep SQL code smaller, by conserving memory.

More Related Content

What's hot (20)

PPTX
Inner join and outer join
Nargis Ehsan
 
PPTX
SQL commands
GirdharRatne
 
PPTX
Joins And Its Types
Wings Interactive
 
PPT
Constraints In Sql
Anurag
 
PPTX
Database constraints
Khadija Parween
 
PPTX
Presentation slides of Sequence Query Language (SQL)
Punjab University
 
PDF
SQL Joins With Examples | Edureka
Edureka!
 
PPTX
DATABASE CONSTRAINTS
sunanditaAnand
 
PPT
Introduction to-sql
BG Java EE Course
 
PPT
Joins in SQL
Vigneshwaran Sankaran
 
PPTX
Sql commands
Pooja Dixit
 
PPT
6. Integrity and Security in DBMS
koolkampus
 
PPTX
Aggregate functions in SQL.pptx
SherinRappai
 
PPTX
Join
Kanchana Rani G
 
PPTX
Integrity Constraints
Megha yadav
 
PPT
Relational algebra operations
SanthiNivas
 
PPTX
Referential integrity
Jubin Raju
 
PPTX
MYSQL join
Ahmed Farag
 
Inner join and outer join
Nargis Ehsan
 
SQL commands
GirdharRatne
 
Joins And Its Types
Wings Interactive
 
Constraints In Sql
Anurag
 
Database constraints
Khadija Parween
 
Presentation slides of Sequence Query Language (SQL)
Punjab University
 
SQL Joins With Examples | Edureka
Edureka!
 
DATABASE CONSTRAINTS
sunanditaAnand
 
Introduction to-sql
BG Java EE Course
 
Joins in SQL
Vigneshwaran Sankaran
 
Sql commands
Pooja Dixit
 
6. Integrity and Security in DBMS
koolkampus
 
Aggregate functions in SQL.pptx
SherinRappai
 
Integrity Constraints
Megha yadav
 
Relational algebra operations
SanthiNivas
 
Referential integrity
Jubin Raju
 
MYSQL join
Ahmed Farag
 

Viewers also liked (10)

PPTX
Sql joins
Gaurav Dhanwant
 
PPTX
Cap 7. oracle SQL fundamentals
Alexander Calderón
 
PDF
Join
Josue Lopez
 
PPTX
Introducción Procesador Consultas SQL Server - Query Processor
Eduardo Castro
 
PPTX
Join
julicasrgar13
 
PDF
Taller básico de JOINS, SUBQUERYING, APPLY, CTE
Julián Castiblanco
 
PPSX
Curso SQL - Leccion 7
Emmanuel Ortiz Gutierrez
 
DOC
SQL Joins
Paul Harkins
 
PDF
SQL Joins and Query Optimization
Brian Gallagher
 
PPTX
Inner join
Israel Rey
 
Sql joins
Gaurav Dhanwant
 
Cap 7. oracle SQL fundamentals
Alexander Calderón
 
Introducción Procesador Consultas SQL Server - Query Processor
Eduardo Castro
 
Taller básico de JOINS, SUBQUERYING, APPLY, CTE
Julián Castiblanco
 
Curso SQL - Leccion 7
Emmanuel Ortiz Gutierrez
 
SQL Joins
Paul Harkins
 
SQL Joins and Query Optimization
Brian Gallagher
 
Inner join
Israel Rey
 
Ad

Similar to Join sql (20)

PPTX
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
PPT
Joins.ppt
UmangThakkar26
 
PPT
Les05 (Displaying Data from Multiple Table)
Achmad Solichin
 
PPT
Les05
Sudharsan S
 
PPT
Les04
Vijay Kumar
 
PPTX
introduction to supply chain management for excel
samantarana1
 
PDF
Assignment 3
SneaK3
 
PPT
App C
Sudharsan S
 
PPT
1 introduction to my sql
Prof. Erwin Globio
 
PPTX
PRESENTATION........................pptx
ejazayesha485
 
PPT
Displaying data from multiple tables
Syed Zaid Irshad
 
PPTX
MergeResult_2024_02_09_08_59_11.pptx
KrishnansuSenapati
 
PPTX
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
PPT
Ms sql server ii
Iblesoft
 
PPTX
Lab4 join - all types listed
Balqees Al.Mubarak
 
PPTX
Sql slid
pacatarpit
 
PPT
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
PPTX
SQL Server Joins.pptx the concept of joins in the oracle and mysql in dbms
narasimharaoPV1
 
PPTX
Practical Tutorial about the PostgreSQL Database
sistemashcp
 
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
Joins.ppt
UmangThakkar26
 
Les05 (Displaying Data from Multiple Table)
Achmad Solichin
 
introduction to supply chain management for excel
samantarana1
 
Assignment 3
SneaK3
 
1 introduction to my sql
Prof. Erwin Globio
 
PRESENTATION........................pptx
ejazayesha485
 
Displaying data from multiple tables
Syed Zaid Irshad
 
MergeResult_2024_02_09_08_59_11.pptx
KrishnansuSenapati
 
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
Ms sql server ii
Iblesoft
 
Lab4 join - all types listed
Balqees Al.Mubarak
 
Sql slid
pacatarpit
 
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
SQL Server Joins.pptx the concept of joins in the oracle and mysql in dbms
narasimharaoPV1
 
Practical Tutorial about the PostgreSQL Database
sistemashcp
 
Ad

More from Vikas Gupta (6)

PPT
SQL DDL
Vikas Gupta
 
PPT
Sql DML
Vikas Gupta
 
PPT
Sql DML
Vikas Gupta
 
PPT
SQL subquery
Vikas Gupta
 
PPT
SQL select statement and functions
Vikas Gupta
 
PPT
Sql intro
Vikas Gupta
 
SQL DDL
Vikas Gupta
 
Sql DML
Vikas Gupta
 
Sql DML
Vikas Gupta
 
SQL subquery
Vikas Gupta
 
SQL select statement and functions
Vikas Gupta
 
Sql intro
Vikas Gupta
 

Recently uploaded (20)

PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
July Patch Tuesday
Ivanti
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 

Join sql

  • 1. Chapter - 4 Displaying Data from Multiple Tables Joins
  • 2. Obtaining Data from Multiple Tables
  • 3. Cartesian Products  When a join condition is invalid or omitted completely, the result is a Cartesian product, in which all combinations of rows are displayed.  All rows in the first table are joined to all rows in the second table.  A Cartesian product tends to generate a large number of rows, and the result is rarely useful.  You should always include a valid join condition in a WHERE clause, unless you have a specific need to combine all rows from all tables.  Cartesian products are useful for some tests when you need to generate a large number of rows to simulate a reasonable amount of data.
  • 6. Joining Tables Using Oracle Syntax  Use a join to query data from more than one table.  Write the join condition in the WHERE clause.  Prefix the column name with the table name when the same column name appears in more than one table.
  • 7. Defining Joins Guidelines    When data from more than one table in the database is required, a join condition is used. Rows in one table can be joined to rows in another table according to common values existing in corresponding columns, that is, usually primary and foreign key columns. To display data from two or more related tables, write a simple join condition in the WHERE clause.  When writing a SELECT statement that joins tables, precede the column name with the table name for clarity and to enhance database access.  If the same column name appears in more than one table, the column name must be prefixed with the table name.  To join n tables together, you need a minimum of n-1 join conditions.  For example, to join four tables, a minimum of three joins is required.  This rule may not apply if your table has a concatenated primary key, in which case more than one column is required to uniquely identify each row.
  • 8. What is an Equi-join?
  • 9. Equijoins  To determine an employee’s department name, you compare the value in the DEPARTMENT_ID column in the EMPLOYEES table with the DEPARTMENT_ID values in the DEPARTMENTS table.  The relationship between the EMPLOYEES and DEPARTMENTS tables is an equijoin—that is, values in the DEPARTMENT_ID column on both tables must be equal.  Frequently, this type of join involves primary and foreign key complements.  Note: Equijoins are also called simple joins or inner joins.
  • 10. Decision Matrix  Explain the use of a decision matrix for simplifying writing joins.  For example, if you want to display the name and department number of all the employees who are in the same department as Goyal, you can start by making the following decision tree:  Now the SQL statement can be easily formulated by looking at the decision matrix.  The first column gives the column list in the SELECT statement, the second column gives the tables for the FROM clause, and the third column gives the condition for the WHERE clause.
  • 11. Qualifying Ambiguous Column Names Guidelines Table aliases can be max. up to 30 characters in length, but shorter is better.   Use table prefixes to qualify column names that are in multiple tables.    If a table alias is used for a particular table name in the FROM clause, then that table alias must be substituted for the table name throughout the SELECT statement.  Table aliases should be meaningful.  The table alias is valid only for the current SELECT statement. Distinguish columns that have identical names but reside in different tables by using column aliases. Using Table Aliases  Simplify queries by using table aliases.  Improve performance by using table prefixes.
  • 12. Joining More than Two Tables
  • 13. Non-Equijoins A non-equijoin is a join condition containing something other than an equality operator.
  • 15. Outer Joins Syntax  You use an outer join to also see rows that do not meet the join condition.  The Outer join operator is the plus sign (+). (+) is placed on the “side” of the join that is deficient in information.
  • 16. Self Joins  Sometimes you need to join a table to itself.
  • 17. Joining Tables Using SQL: 1999 Syntax          table1.column CROSS JOIN NATURAL JOIN JOIN table USING JOIN table ON table1.column_name = table2.column_name LEFT/RIGHT/FULL OUTER Denotes the table and column from which data is retrieved Returns a Cartesian product from the two tables Joins two tables based on the same column name column_name Performs an equijoin based on the column name Performs an equijoin based on the condition in the ON clause
  • 18. Creating Cross Joins  The CROSS JOIN clause produces the cross product of two tables.  This is the same as a Cartesian product between the two tables.
  • 19. Creating Natural Joins  The NATURAL JOIN clause is based on all columns in the two tables that have the same name.  It selects rows from the two tables that have equal values in all matched columns.  If the columns having the same names have different data types, an error is returned.
  • 20. Creating Joins with the USING Clause  If several columns have the same names but the data types do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equi-join.  Use the USING clause to match only one column when more than one column matches.  Do not use a table name or alias in the referenced columns.  The NATURAL JOIN and USING clauses are mutually exclusive.
  • 21. Creating Joins with the ON Clause  The join condition for the natural join is basically an equi-join of all columns with the same name.  To specify arbitrary conditions or specify columns to join, the ON clause is used.  The join condition is separated from other search conditions.  The ON clause makes code easy to understand.
  • 22. Creating Three-Way Joins with the ON Clause
  • 23. INNER Joins Versus OUTER Joins  In SQL: 1999, the join of two tables returning only matched rows is an inner join.  A join between two tables that returns the results of the inner join as well as unmatched rows left (or right) tables is a left (or right) outer join.  A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join.
  • 24. Joins - Comparing SQL: 1999 to Oracle Syntax
  • 25. LEFT OUTER JOIN  This query retrieves all rows in the EMPLOYEES table, which is the left table even if there is no match in the DEPARTMENTS table. This query was completed in earlier releases as follows: SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE d.department_id (+) = e.department_id;
  • 26. RIGHT OUTER JOIN  This query retrieves all rows in the DEPARTMENTS table, which is the right table even if there is no match in the EMPLOYEES table. This query was completed in earlier releases as follows: SELECT e.last_name,e.department_id,d.department_name FROM employees e, departments d WHERE d.department_id = e.department_id (+);
  • 27. FULL OUTER JOIN  This query retrieves all rows in the EMPLOYEES table, even if there is no match in the DEPARTMENTS table. It also retrieves all rows in the DEPARTMENTS table, even if there is no match in the EMPLOYEES table.  It was not possible to complete this in earlier releases using outer joins. However, you could accomplish the same results using the UNION operator. SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id (+) = d.department_id UNION SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id (+);
  • 29. Summary  There are multiple ways to join tables. Types of Joins        Equijoins Non-equijoins Outer joins Self joins Cross joins Natural joins Full or outer joins Cartesian Products A Cartesian product results in all combinations of rows displayed. This is done by either omitting the WHERE clause or specifying the CROSS JOIN clause. Table Aliases   Table aliases speed up database access. Table aliases can help to keep SQL code smaller, by conserving memory.