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

Inner join and outer join
Inner join and outer joinInner join and outer join
Inner join and outer join
Nargis Ehsan
 

What's hot (20)

Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
SQL Join Basic
SQL Join BasicSQL Join Basic
SQL Join Basic
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
MySql:Introduction
MySql:IntroductionMySql:Introduction
MySql:Introduction
 
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
 
Join
JoinJoin
Join
 
Sql joins
Sql joinsSql joins
Sql joins
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
joins in database
 joins in database joins in database
joins in database
 
Join query
Join queryJoin query
Join query
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
SQL
SQLSQL
SQL
 
Inner join and outer join
Inner join and outer joinInner join and outer join
Inner join and outer join
 

Similar to MySQL JOINS

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
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
sagaroceanic11
 

Similar to MySQL JOINS (20)

V19 join method-c
V19 join method-cV19 join method-c
V19 join method-c
 
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
 
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)
 
Sql joins
Sql joinsSql joins
Sql 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 inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
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

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
Krashi Coaching
 

Recently uploaded (20)

The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING IIII BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
The Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptxThe Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptx
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomImplanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 

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.