SlideShare a Scribd company logo
1 of 19
JOINS
NABAGESERA NULU
MBADHI BARNABAS
definitions
A JOIN is a means for combining fields from two tables by
using values common to each
• SQL joins are used to combine rows from two or more
tables.
OR
• An SQL JOIN clause is used to combine rows from two or
more tables, based on a common field between them.
Type of SQL JOINs
• INNER JOIN: Returns all rows when there is at least one match in
BOTH tables
• LEFT JOIN: Return all rows from the left table, and the matched
rows from the right table
• RIGHT JOIN: Return all rows from the right table, and the matched
rows from the left table
• FULL JOIN: Return all rows when there is a match in ONE of the
tables
• SELF JOIN: Returns table to itself as if the table were two tables;
temporarily renaming at least one table in the SQL statement.
• CARTESIAN JOIN: Returns the Cartesian product of the sets of
records from two or more joined tables
SQL INNER JOIN
• The INNER JOIN keyword selects all rows from
both tables as long as there is a match between
the columns in both tables.
• SQL INNER JOIN Syntax
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
• Lets look at the tables on the preceding slides
SQL INNER JOIN
OrderID CustomerID OrderDate
101 1 2019-03-18
102 2 2019-03-19
103 3 2019-03-20
104 null 2019-03-21
CustomerID CustomerName ContactName district
1 Olumonde Godie wakiso
2 Gamukama Raymond luwero
3 Nabacwa Milica kampala
4 Kahinda Adon null
Customers table:
orders table:
SQL INNER JOIN
The relationship between the two tables above is the "CustomerID"
column.
• Then, if we run the following SQL statement (that contains an INNER
JOIN):
Example
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
SQL INNER JOIN
• Output would be
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 2019-03-18
2 Gamukama Raymond luwero 102 2019-03-19
3 Nabacwa Milica kampala 103 2019-03-20
Note: (It selects all rows from both tables as long
as there is a match between the columns in both
tables)
.
SQL LEFT JOIN Keyword
• The LEFT JOIN keyword returns all rows from the left
table (table1), with the matching rows in the right table
(table2). The result is NULL in the right side when there
is no match.
SQL LEFT JOIN Syntax
• SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
• In some databases LEFT OUTER JOIN is called LEFT
JOIN.
Using our tables above,
The following SQL statement will return all customers, and
any orders they might have:
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
SQL LEFT JOIN Example
Note: The LEFT JOIN keyword returns all the rows from the left table
(Customers), even if there are no matches in the right table (Orders).
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 3/18/2019
2 Gamukama Raymond luwero 102 3/19/2019
3 Nabacwa Milica kampala 103 3/20/2019
4 Kahinda Adon null null null
SQL RIGHT JOIN Keyword
• The RIGHT JOIN keyword returns all rows from the
right table (table2), with the matching rows in the left
table (table1). The result is NULL in the left side when
there is no match.
• SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
NB:In some databases RIGHT OUTER JOIN is called
RIGHT JOIN.
Using our tables above,
The following SQL statement will return all employees, and
any orders they have placed:
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
SQL RIGHT JOIN Example
• Note: The RIGHT JOIN keyword returns all the rows from the right table
(Employees), even if there are no matches in the left table (Orders).
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 3/18/2019
2 Gamukama Raymond luwero 102 3/19/2019
3 Nabacwa Milica kampala 103 3/20/2019
null null null null 104 3/21/2019
SQL FULL OUTER JOIN Keyword
• The FULL OUTER JOIN keyword returns all rows
from the left table (table1) and from the right table
(table2).
• To achieve that, we use the join keyword
• The JOIN keyword combines the result of both LEFT
and RIGHT joins.
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
SQL FULL OUTER JOIN Example
• Using our tables above,
• The following SQL statement selects all customers, and all orders:
SELECT
Customers.CustomerID,
Customers.CustomerName,
Customers.ContactName,
Customers.district,
Orders.OrderID,
Orders.OrderDate
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
Note: The JOIN keyword returns all the rows from the left table (Customers), and all the rows from the
right table (Orders). If there are rows in "Customers" that do not have matches in "Orders", or if there are
rows in "Orders" that do not have matches in "Customers", those rows will be listed as well.
CustomerID CustomerName ContactName district OrderID OrderDate
1 Olumonde Godie wakiso 101 3/18/2019
2 Gamukama Raymond luwero 102 3/19/2019
3 Nabacwa Milica kampala 103 3/20/2019
4 Kahinda Adon null null null
null null null null 104 3/21/2019
SELF JOIN
• is used to join a table to itself as if the table were two
tables; temporarily renaming at least one table in the SQL
statement.
SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_field = b.common_field;
Here, the WHERE clause could be any given expression
based on your requirement, for example.(a.value>b.value)
CARTESIAN JOIN
• it returns the Cartesian product of the sets of records from
two or more joined tables. Thus, it equates to an inner join
where the join-condition always evaluates to either True
or where the join-condition is absent from the statement.
SELECT table1.column1, table2.column2...
FROM table1, table2 ;
THANK YOU FOR ATTENDING

More Related Content

What's hot

MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesDataminingTools Inc
 
Excel 2013 Unit A
Excel 2013 Unit AExcel 2013 Unit A
Excel 2013 Unit Ajarana00
 
Intro to Excel Basics: Part II
Intro to Excel Basics: Part IIIntro to Excel Basics: Part II
Intro to Excel Basics: Part IISi Krishan
 
SQL Joinning.Database
SQL Joinning.DatabaseSQL Joinning.Database
SQL Joinning.DatabaseUmme habiba
 
Worksheet Basics & Navigation - Excel 2013 Tutorial
Worksheet Basics & Navigation - Excel 2013 TutorialWorksheet Basics & Navigation - Excel 2013 Tutorial
Worksheet Basics & Navigation - Excel 2013 TutorialSpreadsheetTrainer
 
joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysisSanSan149
 
Intermediate ms excel for business elective course for dlsu-d hs
Intermediate ms excel for business   elective course for dlsu-d hsIntermediate ms excel for business   elective course for dlsu-d hs
Intermediate ms excel for business elective course for dlsu-d hsMarkFreudBolima
 
Excel 2013 Unit B
Excel 2013 Unit BExcel 2013 Unit B
Excel 2013 Unit Bjarana00
 
Intro to Excel Basics: Part I
Intro to Excel Basics: Part IIntro to Excel Basics: Part I
Intro to Excel Basics: Part ISi Krishan
 

What's hot (20)

Join query
Join queryJoin query
Join query
 
Joins And Its Types
Joins And Its TypesJoins And Its Types
Joins And Its Types
 
SQL
SQLSQL
SQL
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
SQL Join's
SQL Join'sSQL Join's
SQL Join's
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
MySQL JOIN & UNION
MySQL JOIN & UNIONMySQL JOIN & UNION
MySQL JOIN & UNION
 
Excel 2013 Unit A
Excel 2013 Unit AExcel 2013 Unit A
Excel 2013 Unit A
 
SQL UNION
SQL UNIONSQL UNION
SQL UNION
 
Sql joins
Sql joinsSql joins
Sql joins
 
Intro to Excel Basics: Part II
Intro to Excel Basics: Part IIIntro to Excel Basics: Part II
Intro to Excel Basics: Part II
 
SQL Joinning.Database
SQL Joinning.DatabaseSQL Joinning.Database
SQL Joinning.Database
 
Sql joins
Sql joinsSql joins
Sql joins
 
Worksheet Basics & Navigation - Excel 2013 Tutorial
Worksheet Basics & Navigation - Excel 2013 TutorialWorksheet Basics & Navigation - Excel 2013 Tutorial
Worksheet Basics & Navigation - Excel 2013 Tutorial
 
joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysis
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
Intermediate ms excel for business elective course for dlsu-d hs
Intermediate ms excel for business   elective course for dlsu-d hsIntermediate ms excel for business   elective course for dlsu-d hs
Intermediate ms excel for business elective course for dlsu-d hs
 
Excel 2013 Unit B
Excel 2013 Unit BExcel 2013 Unit B
Excel 2013 Unit B
 
Intro to Excel Basics: Part I
Intro to Excel Basics: Part IIntro to Excel Basics: Part I
Intro to Excel Basics: Part I
 

Similar to Sql joins final

Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptxSherinRappai
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptxSherinRappai1
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive TechandMate
 
Advance database system(part 8)
Advance database system(part 8)Advance database system(part 8)
Advance database system(part 8)Abdullah Khosa
 
Joins and Views.pptx
Joins and Views.pptxJoins and Views.pptx
Joins and Views.pptxSangitaKabi
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsAshwin Dinoriya
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL FundamentalsBrian Foote
 
Lesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfLesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfHasankaWijesinghe1
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview QuestionsBest SEO Tampa
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxjainendraKUMAR55
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfAnishurRehman1
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)Ishucs
 

Similar to Sql joins final (20)

SQL JOIN.pptx
SQL JOIN.pptxSQL JOIN.pptx
SQL JOIN.pptx
 
Sql wksht-6
Sql wksht-6Sql wksht-6
Sql wksht-6
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Advance database system(part 8)
Advance database system(part 8)Advance database system(part 8)
Advance database system(part 8)
 
Joins and Views.pptx
Joins and Views.pptxJoins and Views.pptx
Joins and Views.pptx
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Lesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfLesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdf
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
 
Unions and joins in mysql
Unions and joins in mysqlUnions and joins in mysql
Unions and joins in mysql
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
Unit_9.pptx
Unit_9.pptxUnit_9.pptx
Unit_9.pptx
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdf
 
Sql
SqlSql
Sql
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 

Recently uploaded

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

Sql joins final

  • 2. definitions A JOIN is a means for combining fields from two tables by using values common to each • SQL joins are used to combine rows from two or more tables. OR • An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
  • 3. Type of SQL JOINs • INNER JOIN: Returns all rows when there is at least one match in BOTH tables • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table • FULL JOIN: Return all rows when there is a match in ONE of the tables • SELF JOIN: Returns table to itself as if the table were two tables; temporarily renaming at least one table in the SQL statement. • CARTESIAN JOIN: Returns the Cartesian product of the sets of records from two or more joined tables
  • 4. SQL INNER JOIN • The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables. • SQL INNER JOIN Syntax • SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name; • Lets look at the tables on the preceding slides
  • 5. SQL INNER JOIN OrderID CustomerID OrderDate 101 1 2019-03-18 102 2 2019-03-19 103 3 2019-03-20 104 null 2019-03-21 CustomerID CustomerName ContactName district 1 Olumonde Godie wakiso 2 Gamukama Raymond luwero 3 Nabacwa Milica kampala 4 Kahinda Adon null Customers table: orders table:
  • 6. SQL INNER JOIN The relationship between the two tables above is the "CustomerID" column. • Then, if we run the following SQL statement (that contains an INNER JOIN): Example SELECT Customers.CustomerID, Customers.CustomerName, Customers.ContactName, Customers.district, Orders.OrderID, Orders.OrderDate FROM Customers INNER JOIN Orders ON Customers.CustomerID=Orders.CustomerID;
  • 7. SQL INNER JOIN • Output would be CustomerID CustomerName ContactName district OrderID OrderDate 1 Olumonde Godie wakiso 101 2019-03-18 2 Gamukama Raymond luwero 102 2019-03-19 3 Nabacwa Milica kampala 103 2019-03-20 Note: (It selects all rows from both tables as long as there is a match between the columns in both tables) .
  • 8. SQL LEFT JOIN Keyword • The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match. SQL LEFT JOIN Syntax • SELECT column_name(s) FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name; • In some databases LEFT OUTER JOIN is called LEFT JOIN.
  • 9. Using our tables above, The following SQL statement will return all customers, and any orders they might have: SELECT Customers.CustomerID, Customers.CustomerName, Customers.ContactName, Customers.district, Orders.OrderID, Orders.OrderDate FROM Customers LEFT JOIN Orders ON Customers.CustomerID=Orders.CustomerID;
  • 10. SQL LEFT JOIN Example Note: The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no matches in the right table (Orders). CustomerID CustomerName ContactName district OrderID OrderDate 1 Olumonde Godie wakiso 101 3/18/2019 2 Gamukama Raymond luwero 102 3/19/2019 3 Nabacwa Milica kampala 103 3/20/2019 4 Kahinda Adon null null null
  • 11. SQL RIGHT JOIN Keyword • The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match. • SQL RIGHT JOIN Syntax SELECT column_name(s) FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name=table2.column_name; NB:In some databases RIGHT OUTER JOIN is called RIGHT JOIN.
  • 12. Using our tables above, The following SQL statement will return all employees, and any orders they have placed: SELECT Customers.CustomerID, Customers.CustomerName, Customers.ContactName, Customers.district, Orders.OrderID, Orders.OrderDate FROM Customers RIGHT JOIN Orders ON Customers.CustomerID=Orders.CustomerID;
  • 13. SQL RIGHT JOIN Example • Note: The RIGHT JOIN keyword returns all the rows from the right table (Employees), even if there are no matches in the left table (Orders). CustomerID CustomerName ContactName district OrderID OrderDate 1 Olumonde Godie wakiso 101 3/18/2019 2 Gamukama Raymond luwero 102 3/19/2019 3 Nabacwa Milica kampala 103 3/20/2019 null null null null 104 3/21/2019
  • 14. SQL FULL OUTER JOIN Keyword • The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2). • To achieve that, we use the join keyword • The JOIN keyword combines the result of both LEFT and RIGHT joins. SQL FULL OUTER JOIN Syntax SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name=table2.column_name;
  • 15. SQL FULL OUTER JOIN Example • Using our tables above, • The following SQL statement selects all customers, and all orders: SELECT Customers.CustomerID, Customers.CustomerName, Customers.ContactName, Customers.district, Orders.OrderID, Orders.OrderDate FROM Customers RIGHT JOIN Orders ON Customers.CustomerID=Orders.CustomerID;
  • 16. Note: The JOIN keyword returns all the rows from the left table (Customers), and all the rows from the right table (Orders). If there are rows in "Customers" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Customers", those rows will be listed as well. CustomerID CustomerName ContactName district OrderID OrderDate 1 Olumonde Godie wakiso 101 3/18/2019 2 Gamukama Raymond luwero 102 3/19/2019 3 Nabacwa Milica kampala 103 3/20/2019 4 Kahinda Adon null null null null null null null 104 3/21/2019
  • 17. SELF JOIN • is used to join a table to itself as if the table were two tables; temporarily renaming at least one table in the SQL statement. SELECT a.column_name, b.column_name... FROM table1 a, table1 b WHERE a.common_field = b.common_field; Here, the WHERE clause could be any given expression based on your requirement, for example.(a.value>b.value)
  • 18. CARTESIAN JOIN • it returns the Cartesian product of the sets of records from two or more joined tables. Thus, it equates to an inner join where the join-condition always evaluates to either True or where the join-condition is absent from the statement. SELECT table1.column1, table2.column2... FROM table1, table2 ;
  • 19. THANK YOU FOR ATTENDING