SlideShare a Scribd company logo
1 of 18
Download to read offline
MySQL JOINS
What are JOINS?
 Joins help retrieving data from two or more database tables.
 The tables are mutually related using primary and foreign keys.
MySQL Cross Join
 Cross JOIN is a simplestform of JOINs which matches each row from one databasetable to all
rows of another.
 In other words it gives us combinations of each row of first table with all records in second table.
Cross Join
All rows from both tables
MySQL Cross Join
 All mentioned bellow lines will produce the same result:
SELECT * FROM table_1 CROSS JOIN table_2;
OR
SELECT table_1.id, table_1.greeting, table_2.id, table_2.question FROM table_1 CROSS JOIN table_2;
OR
SELECT table_1.id, table_1.greeting, table_2.id, table_2.question FROM table_1 , table_2;
MySQL Cross Join
id greeting
1 hello
2 hi
id question
1 How are you doing?
2 Are you there?
table_1 table_2
SELECT * FROM table_1 CROSS JOIN table_2;
Result
1
2
1
2
MySQL Inner Join
 The inner JOIN is used to return rows from both tables that satisfy the given condition.
 The inner JOIN is same as JOIN clause, combining rows from two or more tables.
Inner Join
Only matching rows
MySQL Inner Join
 Both lines will produce the same result:
SELECT * FROM table_1 INNER JOIN table_2 ON table_1.id = table_2.id;
OR
SELECT * FROM table_1 JOIN table_2 ON table_1.id = table_2.id;;
MySQL Inner Join
id greeting
1 hello
2 hi
id question
1 How are you doing?
2 Are you there?
table_1 table_2
SELECT * FROM table_1 INNER JOIN table_2 ON table_1.id = table_2.id;
Result
MySQL Outer Join
 MySQL does not support outer join.
MySQL Left Join
 The LEFT JOIN returns all the rows from the table on the left even if no matching rows
have been found in the table on the right.
 Where no matches have been found in the table on the right, NULL is returned
Left Join
All rows from the left table
MySQL Left Join
 Both lines will produce the same result:
SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.id = table_2.id;
OR
SELECT * FROM table_1 LEFT OUTER JOIN table_2 ON table_1.id = table_2.id;;
MySQL Left Join
id greeting
1 hello
2 hi
3 hey
id question
1 How are you doing?
2 Are you there?
table_1 table_2
SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.id = table_2.id;
Result
MySQL Right Join
 The RIGHT JOIN returns all the columns from the table on the right even if no matching
rows have been found in the table on the left.
 Where no matches have been found in the table on the left, NULL is returned.
Right Join
All rows from the right table
MySQL Right Join
 Both lines will produce the same result:
SELECT * FROM table_1 RIGHT JOIN table_2 ON table_1.id = table_2.id;
OR
SELECT * FROM table_1 RIGHT OUTER JOIN table_2 ON table_1.id = table_2.id;;
MySQL Right Join
id greeting
1 hello
2 hi
id question
1 How are you doing?
2 Are you there?
3 Are you ready?
table_1 table_2
SELECT * FROM table_1 RIGHT JOIN table_2 ON table_1.id = table_2.id;
Result
MySQL Right Join with USING clause
id question
1 How are you doing?
2 Are you there?
3 Are you ready?
table_1 table_2
SELECT * FROM table_1 RIGHT JOIN table_2 USING(id);
Result
id greeting
1 hello
2 hi
Why we use JOINs
 Using JOINs, you can get the work done by using only a one query with any search
parameters.
 MySQL can achieve better performance with JOINs as it can use Indexing. Simply use of single
JOIN query instead running multiple queries do reduce server overhead. Using multiple
queries instead that leads more data transfers between MySQL and applications (software).
Further it requires more data manipulations in application end also.
MySQL JOINS

More Related Content

What's hot (20)

Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Joins And Its Types
Joins And Its TypesJoins And Its Types
Joins And Its Types
 
Joins
JoinsJoins
Joins
 
SQL UNION
SQL UNIONSQL UNION
SQL UNION
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
joins in database
 joins in database joins in database
joins in database
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Sql join
Sql  joinSql  join
Sql join
 
Sql joins
Sql joinsSql joins
Sql joins
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 

Similar to MySQL JOINS

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
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)Ishucs
 
Join in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer JoinJoin in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer JoinSouma Maiti
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive TechandMate
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)Ehtisham Ali
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joinsMudasir Syed
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01sagaroceanic11
 
Joins and Views.pptx
Joins and Views.pptxJoins and Views.pptx
Joins and Views.pptxSangitaKabi
 
Relational algebra
Relational algebraRelational algebra
Relational algebraJoshi Vinay
 
Sql joins final
Sql joins finalSql joins final
Sql joins finalmbadhi
 
Using mySQL in PHP
Using mySQL in PHPUsing mySQL in PHP
Using mySQL in PHPMike Crabb
 

Similar to MySQL JOINS (20)

V19 join method-c
V19 join method-cV19 join method-c
V19 join method-c
 
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
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Join query
Join queryJoin query
Join query
 
Joining
JoiningJoining
Joining
 
Join in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer JoinJoin in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer Join
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
SQL JOIN.pptx
SQL JOIN.pptxSQL JOIN.pptx
SQL JOIN.pptx
 
types of SQL Joins
 types of SQL Joins types of SQL Joins
types of SQL Joins
 
JOINS.pptx
JOINS.pptxJOINS.pptx
JOINS.pptx
 
database .pptx
database .pptxdatabase .pptx
database .pptx
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joins
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
 
Joins and Views.pptx
Joins and Views.pptxJoins and Views.pptx
Joins and Views.pptx
 
Sql joins
Sql joinsSql joins
Sql joins
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Sql joins final
Sql joins finalSql joins final
Sql joins final
 
Using mySQL in PHP
Using mySQL in PHPUsing mySQL in PHP
Using mySQL in PHP
 

Recently uploaded

ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
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
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
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
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
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
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
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
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
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
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
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
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

MySQL JOINS

  • 2. What are JOINS?  Joins help retrieving data from two or more database tables.  The tables are mutually related using primary and foreign keys.
  • 3. MySQL Cross Join  Cross JOIN is a simplestform of JOINs which matches each row from one databasetable to all rows of another.  In other words it gives us combinations of each row of first table with all records in second table. Cross Join All rows from both tables
  • 4. MySQL Cross Join  All mentioned bellow lines will produce the same result: SELECT * FROM table_1 CROSS JOIN table_2; OR SELECT table_1.id, table_1.greeting, table_2.id, table_2.question FROM table_1 CROSS JOIN table_2; OR SELECT table_1.id, table_1.greeting, table_2.id, table_2.question FROM table_1 , table_2;
  • 5. MySQL Cross Join id greeting 1 hello 2 hi id question 1 How are you doing? 2 Are you there? table_1 table_2 SELECT * FROM table_1 CROSS JOIN table_2; Result 1 2 1 2
  • 6. MySQL Inner Join  The inner JOIN is used to return rows from both tables that satisfy the given condition.  The inner JOIN is same as JOIN clause, combining rows from two or more tables. Inner Join Only matching rows
  • 7. MySQL Inner Join  Both lines will produce the same result: SELECT * FROM table_1 INNER JOIN table_2 ON table_1.id = table_2.id; OR SELECT * FROM table_1 JOIN table_2 ON table_1.id = table_2.id;;
  • 8. MySQL Inner Join id greeting 1 hello 2 hi id question 1 How are you doing? 2 Are you there? table_1 table_2 SELECT * FROM table_1 INNER JOIN table_2 ON table_1.id = table_2.id; Result
  • 9. MySQL Outer Join  MySQL does not support outer join.
  • 10. MySQL Left Join  The LEFT JOIN returns all the rows from the table on the left even if no matching rows have been found in the table on the right.  Where no matches have been found in the table on the right, NULL is returned Left Join All rows from the left table
  • 11. MySQL Left Join  Both lines will produce the same result: SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.id = table_2.id; OR SELECT * FROM table_1 LEFT OUTER JOIN table_2 ON table_1.id = table_2.id;;
  • 12. MySQL Left Join id greeting 1 hello 2 hi 3 hey id question 1 How are you doing? 2 Are you there? table_1 table_2 SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.id = table_2.id; Result
  • 13. MySQL Right Join  The RIGHT JOIN returns all the columns from the table on the right even if no matching rows have been found in the table on the left.  Where no matches have been found in the table on the left, NULL is returned. Right Join All rows from the right table
  • 14. MySQL Right Join  Both lines will produce the same result: SELECT * FROM table_1 RIGHT JOIN table_2 ON table_1.id = table_2.id; OR SELECT * FROM table_1 RIGHT OUTER JOIN table_2 ON table_1.id = table_2.id;;
  • 15. MySQL Right Join id greeting 1 hello 2 hi id question 1 How are you doing? 2 Are you there? 3 Are you ready? table_1 table_2 SELECT * FROM table_1 RIGHT JOIN table_2 ON table_1.id = table_2.id; Result
  • 16. MySQL Right Join with USING clause id question 1 How are you doing? 2 Are you there? 3 Are you ready? table_1 table_2 SELECT * FROM table_1 RIGHT JOIN table_2 USING(id); Result id greeting 1 hello 2 hi
  • 17. Why we use JOINs  Using JOINs, you can get the work done by using only a one query with any search parameters.  MySQL can achieve better performance with JOINs as it can use Indexing. Simply use of single JOIN query instead running multiple queries do reduce server overhead. Using multiple queries instead that leads more data transfers between MySQL and applications (software). Further it requires more data manipulations in application end also.