SlideShare a Scribd company logo
1 of 12
Joins
CIS-182
Multi-Table Queries
• Multi-table queries allow data from
different tables to be combined and
compared
• Typically done by comparing a common
value
– Comparison done between like data types
– May be primary key/foreign key fields but
doesn’t have to be
Types of Multi-table Queries
• Joins: combine columns from multiple tables
– Combine publishers and titles to show title and
publisher name
• Unions: combine rows from different lists
– Create a list of editors and authors who live in
California
• Subqueries: use the results of one query to
determine result of a second query
– Find all books that have a higher than average
price
Joins
• Inner Join: create a row for each match of
values from two tables
• Outer Join: include all rows from one table
and any related data found in a second
table
• Cartesian Join: match each row in a table
with every row in a second table
– Also referred to as a cross join
Inner Joins
• Most common kind of join
• Get related data from two tables where
values match
– Also referred to as an equi-join
• May be done using FROM clause or
WHERE clause
– Microsoft does not fully support WHERE
clause in SQL Server 2008
Join With FROM
• Use the keyword JOIN with or without
INNER preceeding it
• Specify the fields to compare using ON
• Field names that are not unique must be
qualified by the table they’re in
From Clause Examples
SELECT *
FROM Publishers JOIN Titles
ON Publishers.pub_id = Titles.pub_id
SELECT *
FROM Publishers INNER JOIN Titles
ON Publishers.pub_id = Titles.pub_id
Join With WHERE
• List tables in FROM clause, separated by
comma
• WHERE clause defines the fields to
compare
Where Clause Example
SELECT *
FROM Publishers, Titles
WHERE Publishers.pub_id =
Titles.pub_id
Outer Joins
• Used when rows from one table should be
part of the result even if there are no
related rows in a second table
– Create a list of all publishers, including any
titles the publisher has published
• Direction must be specified
– Left/Right specify which table has the rows
which should always be included
– Full specifies that rows from both tables
should be included even if no match
Outer Join Example
• Include all publishers, even if they haven’t
published a book:
SELECT *
FROM Publishers LEFT OUTER JOIN
Titles ON Publishers.pub_id =
Titles.pub_id
Cartesian Join
• Match each row from table 1 with every row
from table 2
– Result is (table 1 row count)*(table 2 row count)
• May be done using CROSS JOIN
SELECT *
FROM Publishers, Titles
SELECT *
FROM Publishers CROSS JOIN Titles

More Related Content

Viewers also liked

Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Serverprogrammings guru
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query OptimizationBrian Gallagher
 
Joins – which, when and why
Joins – which, when and whyJoins – which, when and why
Joins – which, when and whyMichal Simonik
 

Viewers also liked (10)

Hadoop Mapreduce joins
Hadoop Mapreduce joinsHadoop Mapreduce joins
Hadoop Mapreduce joins
 
SQL JOINS- Reena P V
SQL JOINS- Reena P VSQL JOINS- Reena P V
SQL JOINS- Reena P V
 
Sql join
Sql  joinSql  join
Sql join
 
SQL JOIN Explained Visually
SQL JOIN Explained VisuallySQL JOIN Explained Visually
SQL JOIN Explained Visually
 
Sql joins
Sql joinsSql joins
Sql joins
 
Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Server
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 
Sql joins
Sql joinsSql joins
Sql joins
 
Joins – which, when and why
Joins – which, when and whyJoins – which, when and why
Joins – which, when and why
 

Similar to Joins (17)

Select Queries
Select QueriesSelect Queries
Select Queries
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingTech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
MIS 226: Chapter 4
MIS 226: Chapter 4MIS 226: Chapter 4
MIS 226: Chapter 4
 
SQL Joins and View.pptx
SQL Joins and View.pptxSQL Joins and View.pptx
SQL Joins and View.pptx
 
Joins and unions
Joins and unionsJoins and unions
Joins and unions
 
Joins and Views.pptx
Joins and Views.pptxJoins and Views.pptx
Joins and Views.pptx
 
Sql
SqlSql
Sql
 
Join sql
Join sqlJoin sql
Join sql
 
Lesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfLesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdf
 
Basics of excel
Basics of excelBasics of excel
Basics of excel
 
Subqueries
SubqueriesSubqueries
Subqueries
 
SQL Join's
SQL Join'sSQL Join's
SQL Join's
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Querying_with_T-SQL_-_03.pptx
Querying_with_T-SQL_-_03.pptxQuerying_with_T-SQL_-_03.pptx
Querying_with_T-SQL_-_03.pptx
 
Querying_with_T-SQL_-_03 (1).pptx
Querying_with_T-SQL_-_03 (1).pptxQuerying_with_T-SQL_-_03 (1).pptx
Querying_with_T-SQL_-_03 (1).pptx
 
Optimizing MySQL queries
Optimizing MySQL queriesOptimizing MySQL queries
Optimizing MySQL queries
 

More from Randy Riness @ South Puget Sound Community College

More from Randy Riness @ South Puget Sound Community College (20)

Stored procedures
Stored proceduresStored procedures
Stored procedures
 
3 sql overview
3 sql overview3 sql overview
3 sql overview
 
Normalization
NormalizationNormalization
Normalization
 
CIS160 final review
CIS160 final reviewCIS160 final review
CIS160 final review
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
CIS 245 Final Review
CIS 245 Final ReviewCIS 245 Final Review
CIS 245 Final Review
 
CIS145 Final Review
CIS145 Final ReviewCIS145 Final Review
CIS145 Final Review
 
Cis166 Final Review C#
Cis166 Final Review C#Cis166 Final Review C#
Cis166 Final Review C#
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
CIS245 sql
CIS245 sqlCIS245 sql
CIS245 sql
 
Cis245 Midterm Review
Cis245 Midterm ReviewCis245 Midterm Review
Cis245 Midterm Review
 
CSS
CSSCSS
CSS
 
XPath
XPathXPath
XPath
 
XSLT Overview
XSLT OverviewXSLT Overview
XSLT Overview
 
Views
ViewsViews
Views
 
CIS282 Midterm review
CIS282 Midterm reviewCIS282 Midterm review
CIS282 Midterm review
 
Schemas 2 - Restricting Values
Schemas 2 - Restricting ValuesSchemas 2 - Restricting Values
Schemas 2 - Restricting Values
 
CIS 145 test 1 review
CIS 145 test 1 reviewCIS 145 test 1 review
CIS 145 test 1 review
 
XML schemas
XML schemasXML schemas
XML schemas
 
Document type definitions part 2
Document type definitions part 2Document type definitions part 2
Document type definitions part 2
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

Joins

  • 2. Multi-Table Queries • Multi-table queries allow data from different tables to be combined and compared • Typically done by comparing a common value – Comparison done between like data types – May be primary key/foreign key fields but doesn’t have to be
  • 3. Types of Multi-table Queries • Joins: combine columns from multiple tables – Combine publishers and titles to show title and publisher name • Unions: combine rows from different lists – Create a list of editors and authors who live in California • Subqueries: use the results of one query to determine result of a second query – Find all books that have a higher than average price
  • 4. Joins • Inner Join: create a row for each match of values from two tables • Outer Join: include all rows from one table and any related data found in a second table • Cartesian Join: match each row in a table with every row in a second table – Also referred to as a cross join
  • 5. Inner Joins • Most common kind of join • Get related data from two tables where values match – Also referred to as an equi-join • May be done using FROM clause or WHERE clause – Microsoft does not fully support WHERE clause in SQL Server 2008
  • 6. Join With FROM • Use the keyword JOIN with or without INNER preceeding it • Specify the fields to compare using ON • Field names that are not unique must be qualified by the table they’re in
  • 7. From Clause Examples SELECT * FROM Publishers JOIN Titles ON Publishers.pub_id = Titles.pub_id SELECT * FROM Publishers INNER JOIN Titles ON Publishers.pub_id = Titles.pub_id
  • 8. Join With WHERE • List tables in FROM clause, separated by comma • WHERE clause defines the fields to compare
  • 9. Where Clause Example SELECT * FROM Publishers, Titles WHERE Publishers.pub_id = Titles.pub_id
  • 10. Outer Joins • Used when rows from one table should be part of the result even if there are no related rows in a second table – Create a list of all publishers, including any titles the publisher has published • Direction must be specified – Left/Right specify which table has the rows which should always be included – Full specifies that rows from both tables should be included even if no match
  • 11. Outer Join Example • Include all publishers, even if they haven’t published a book: SELECT * FROM Publishers LEFT OUTER JOIN Titles ON Publishers.pub_id = Titles.pub_id
  • 12. Cartesian Join • Match each row from table 1 with every row from table 2 – Result is (table 1 row count)*(table 2 row count) • May be done using CROSS JOIN SELECT * FROM Publishers, Titles SELECT * FROM Publishers CROSS JOIN Titles