<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mahendra</title>
    <description>The latest articles on DEV Community by Mahendra (@mahendrad001).</description>
    <link>https://dev.to/mahendrad001</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F849868%2Fe4e67dbb-038c-439e-adef-bfb35fee5d7d.png</url>
      <title>DEV Community: Mahendra</title>
      <link>https://dev.to/mahendrad001</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahendrad001"/>
    <language>en</language>
    <item>
      <title>MySQL Join | Types and Examples</title>
      <dc:creator>Mahendra</dc:creator>
      <pubDate>Tue, 26 Apr 2022 13:22:41 +0000</pubDate>
      <link>https://dev.to/mahendrad001/mysql-join-types-and-examples-3jen</link>
      <guid>https://dev.to/mahendrad001/mysql-join-types-and-examples-3jen</guid>
      <description>&lt;p&gt;This blog talks about the &lt;strong&gt;MySQL Joins&lt;/strong&gt; and different types of MySQL Join with examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is MySQL Join?&lt;/li&gt;
&lt;li&gt;Types of MySQL Joins
2.1 Inner Join
2.2 Left Outer Join
2.3 Right Outer Join&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is MySQL JOIN?
&lt;/h2&gt;

&lt;p&gt;MySQL databases typically hold a lot of information. DBAs and analysts must select records from two or more tables based on certain conditions in order to appropriately examine this data. &lt;a href="https://hevodata.com/learn/mysql-join/"&gt;MySQL Join&lt;/a&gt; come in handy in this situation. Joins allow you to combine data from multiple tables into a single query. The tables must be linked together with a common key value in order for SQL Joins to work.&lt;/p&gt;

&lt;p&gt;One of the most commonly used MySQL Server clauses for extracting and collating data from two or more tables is SQL Joins. Data is arranged in a huge array of tables in a real-world database, necessitating the requirement to combine these many databases based on logical relationships between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of MySQL Joins
&lt;/h2&gt;

&lt;p&gt;There are three primary types of MySQL Joins:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inner Join&lt;/li&gt;
&lt;li&gt;Left Outer Join&lt;/li&gt;
&lt;li&gt;Right Outer Join&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Inner Join
&lt;/h2&gt;

&lt;p&gt;You've probably created a statement that employs a MySQL INNER JOIN before. INNER JOINS in MySQL return all rows from many tables that meet the JOIN requirement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is the syntax:&lt;/strong&gt;&lt;br&gt;
SELECT columns&lt;br&gt;
FROM table1 &lt;br&gt;
INNER JOIN table2&lt;br&gt;
ON table1.column = table2.column;&lt;br&gt;
Here is a visual representation to understand INNER JOINS:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tqo8wx2J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dp9rjkru3416kjiv0x9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tqo8wx2J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dp9rjkru3416kjiv0x9.gif" alt="Inner Join" width="250" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The entries where table1 and table2 intersect would be returned by the MySQL INNER JOIN.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Given below is an example of MySQL INNER JOIN:&lt;/strong&gt;&lt;br&gt;
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date&lt;br&gt;
FROM suppliers &lt;br&gt;
INNER JOIN orders&lt;br&gt;
ON suppliers.supplier_id = orders.supplier_id;&lt;/p&gt;

&lt;p&gt;This MySQL INNER JOIN example would retrieve all rows from the suppliers and orders tables that have the same supplier id value.&lt;/p&gt;

&lt;p&gt;Take a look at some numbers to see how INNER JOINS work:&lt;/p&gt;

&lt;p&gt;“suppliers” is a table with two fields (supplier id and supplier name). It includes the following information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--od0ZasbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9s50i3ujfp0u5qh80fb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--od0ZasbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9s50i3ujfp0u5qh80fb.png" alt="Inner Join Table1" width="361" height="245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“orders” is a separate table with three fields (order id, supplier id, and order date). It includes the following information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rPusjWl5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25s332q9oz16gowkj3u4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rPusjWl5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25s332q9oz16gowkj3u4.png" alt="Inner Join Table2" width="427" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the below code which contains the INNER JOIN statement:&lt;/p&gt;

&lt;p&gt;SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date&lt;br&gt;
FROM suppliers&lt;br&gt;
INNER JOIN orders&lt;br&gt;
ON suppliers.supplier_id = orders.supplier_id;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result set for the above code will be:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---r83pKNR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf10u3umu8t9v8o8bfzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---r83pKNR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf10u3umu8t9v8o8bfzq.png" alt="Inner Join Result" width="512" height="141"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because the supplier ids 10002 and 10003 do not exist in both tables, the rows for Microsoft and NVIDIA from the supplier table would be removed. Because the supplier id 10004 does not exist in the “suppliers” table, the row for 500127 (order id) from the orders table would be removed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Left Outer Join&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A MySQL LEFT OUTER JOIN is another sort of join. All rows from the LEFT-hand table provided in the ON condition are returned, as well as only those rows from the other table where the linked fields are equivalent (join condition is met).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is the syntax:&lt;/strong&gt;&lt;br&gt;
SELECT columns&lt;br&gt;
FROM table1&lt;br&gt;
LEFT [OUTER] JOIN table2&lt;br&gt;
ON table1.column = table2.column;&lt;br&gt;
The LEFT OUTER JOIN keywords are substituted with LEFT JOIN in several databases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is a visual representation to understand LEFT JOINS:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OJGvfeui--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ra3vmc9gfjzfmrz4i6af.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OJGvfeui--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ra3vmc9gfjzfmrz4i6af.gif" alt="Left Join" width="250" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The LEFT OUTER JOIN in MySQL would return all records from table 1 as well as only those from table 2 that intersect with table 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Given below is an example of LEFT OUTER JOIN:&lt;/strong&gt;&lt;br&gt;
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date&lt;br&gt;
FROM suppliers&lt;br&gt;
LEFT JOIN orders&lt;br&gt;
ON suppliers.supplier_id = orders.supplier_id;&lt;/p&gt;

&lt;p&gt;This LEFT OUTER JOIN example would return all rows from the suppliers table and only those rows from the orders table with equal connected fields.&lt;/p&gt;

&lt;p&gt;All fields in the orders table will display as null in the result set if a supplier id value in the suppliers table does not exist in the orders table.&lt;/p&gt;

&lt;p&gt;Let's take a look at some numbers to see how LEFT OUTER JOINS work:&lt;/p&gt;

&lt;p&gt;Suppliers is a table with two fields (supplier id and supplier name). It includes the following information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--es6NS_WQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sq4xty1kig2qgyl9g7iv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--es6NS_WQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sq4xty1kig2qgyl9g7iv.png" alt="Left Join Table1" width="361" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three fields in the orders table (order id, supplier id, and order date). It includes the following information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IaiNEc3I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfq0k5iwwqfpsbqc641p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IaiNEc3I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfq0k5iwwqfpsbqc641p.png" alt="Left Join Table2" width="418" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the below code which contains the LEFT OUTER JOIN statement:&lt;/p&gt;

&lt;p&gt;SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date&lt;br&gt;
FROM suppliers&lt;br&gt;
LEFT JOIN orders&lt;br&gt;
ON suppliers.supplier_id = orders.supplier_id;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result set would be:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c7wiz_of--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6zkrn1cq7d2mbe5rtt3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c7wiz_of--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6zkrn1cq7d2mbe5rtt3.png" alt="Left Join Result" width="512" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because a LEFT OUTER JOIN was utilized, the rows for Microsoft and NVIDIA would be included. You'll notice, though, that the order date field for those records has a null value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Right Outer Join&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A MySQL RIGHT OUTER JOIN is another type of join. All rows from the RIGHT-hand table provided in the ON condition are returned, as well as only those rows from the other table where the linked fields are equivalent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is the syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SELECT columns&lt;br&gt;
FROM table1&lt;br&gt;
RIGHT [OUTER] JOIN table2&lt;br&gt;
ON table1.column = table2.column;&lt;br&gt;
Here is a visual representation of the RIGHT OUTER JOIN:&lt;/p&gt;

&lt;p&gt;The MySQL RIGHT OUTER JOIN returns the shaded area in this visual diagram:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---c8oCgTS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m427so8vmeufxnxbu9g6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---c8oCgTS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m427so8vmeufxnxbu9g6.gif" alt="Right Join" width="250" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The MySQL RIGHT OUTER JOIN returns all records from table 2 as well as only those from table 1 that intersect with table 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Given below is an example of MySQL RIGHT OUTER JOIN:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SELECT orders.order_id, orders.order_date, suppliers.supplier_name&lt;br&gt;
FROM suppliers&lt;br&gt;
RIGHT JOIN orders&lt;br&gt;
ON suppliers.supplier_id = orders.supplier_id;&lt;/p&gt;

&lt;p&gt;This example of a RIGHT OUTER JOIN would return all rows from the orders table as well as only those rows from the suppliers table where the connected fields are equal.&lt;/p&gt;

&lt;p&gt;All fields in the suppliers table will display as null in the result set if a supplier id value in the orders table does not exist in the suppliers table.&lt;/p&gt;

&lt;p&gt;Let's take a look at some numbers to see how RIGHT OUTER JOINS work:&lt;/p&gt;

&lt;p&gt;Suppliers is a table with two fields (supplier id and supplier name). It includes the following information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AZcnFJio--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezvwkbihcqlzvsbux6sa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AZcnFJio--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezvwkbihcqlzvsbux6sa.png" alt="Right Join Table1" width="345" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three fields in the orders table (order id, supplier id, and order date). It includes the following information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wt_DFGlo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bl3mtde5sxv1jzy42y6w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wt_DFGlo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bl3mtde5sxv1jzy42y6w.png" alt="Right Join Table2" width="427" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run the below code which contains the ILEFT OUTER JOIN statement:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SELECT orders.order_id, orders.order_date, suppliers.supplier_name&lt;br&gt;
FROM suppliers&lt;br&gt;
RIGHT JOIN orders&lt;br&gt;
ON suppliers.supplier_id = orders.supplier_id;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The result set will be:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qi2GhfR1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xzovah674vsi51yxhij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qi2GhfR1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xzovah674vsi51yxhij.png" alt="Right Join Result" width="466" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because a RIGHT OUTER JOIN was performed, the record for 500127 (order id) would be included. You'll notice, though, that the supplier name field for that record has a value of null.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You have learned about the three key types of MySQL JOINS along with their respective syntaxes and examples for better understanding. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hevo&lt;/strong&gt; is a No-code Data Pipeline that is producing the industry's most robust and comprehensive ETL solution. At a reasonable price, you can link with your databases, cloud apps, flat files, clickstream, and other systems.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>mysql</category>
    </item>
  </channel>
</rss>
