{"id":3996,"date":"2022-02-09T10:34:31","date_gmt":"2022-02-09T18:34:31","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3996"},"modified":"2022-02-09T10:34:33","modified_gmt":"2022-02-09T18:34:33","slug":"what-is-the-difference-between-inner-join-left-join-right-join-and-full-join","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3996","title":{"rendered":"What is the Difference Between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.stack.imgur.com\/VQ5XP.png\" alt=\"alt text\"\/><\/figure>\n\n\n\n<p>There are different types of joins available in SQL:<\/p>\n\n\n\n<p><strong>INNER JOIN<\/strong>: returns rows when there is a match in both tables.<\/p>\n\n\n\n<p><strong>LEFT JOIN<\/strong>: returns all rows from the left table, even if there are no matches in the right table.<\/p>\n\n\n\n<p><strong>RIGHT JOIN<\/strong>: returns all rows from the right table, even if there are no matches in the left table.<\/p>\n\n\n\n<p><strong>FULL JOIN<\/strong>: combines the results of both left and right outer joins.<\/p>\n\n\n\n<p>The joined table will contain all records from both the tables and fill in NULLs for missing matches on either side.<\/p>\n\n\n\n<p><strong>SELF JOIN<\/strong>: joins a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.<\/p>\n\n\n\n<p><strong>CARTESIAN JOIN<\/strong>: returns the Cartesian product of the sets of records from the two or more joined tables.<\/p>\n\n\n\n<p>We can take each first four joins in Details :<\/p>\n\n\n\n<p>We have two tables with the following values.<\/p>\n\n\n\n<p><strong>TableA<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id  firstName                  lastName\n.......................................\n1   arun                        prasanth                 \n2   ann                         antony                   \n3   sruthy                      abc                      \n6   new                         abc                                           \n<\/code><\/pre>\n\n\n\n<p><strong>TableB<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id2 age Place\n................\n1   24  kerala\n2   24  usa\n3   25  ekm\n5   24  chennai\n<\/code><\/pre>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<\/p>\n\n\n\n<p><strong>INNER JOIN<\/strong><\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;: gives the intersection of the two tables, i.e. rows TableA and TableB have in common.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT table1.column1, table2.column2...\n  FROM table1\n INNER JOIN table2\n    ON table1.common_field = table2.common_field;\n<\/code><\/pre>\n\n\n\n<p>Apply it in our sample table :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place\n  FROM TableA\n INNER JOIN TableB\n    ON TableA.id = TableB.id2;\n<\/code><\/pre>\n\n\n\n<p>Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>firstName       lastName       age  Place\n..............................................\narun            prasanth        24  kerala\nann             antony          24  usa\nsruthy          abc             25  ekm\n<\/code><\/pre>\n\n\n\n<p><strong>LEFT JOIN<\/strong><\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;: gives all selected rows in TableA, plus any common selected rows in TableB.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT table1.column1, table2.column2...\n  FROM table1\n  LEFT JOIN table2\n    ON table1.common_field = table2.common_field;\n<\/code><\/pre>\n\n\n\n<p>Apply it in our sample table :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place\n  FROM TableA\n  LEFT JOIN TableB\n    ON TableA.id = TableB.id2;\n<\/code><\/pre>\n\n\n\n<p>Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>firstName                   lastName                    age   Place\n...............................................................................\narun                        prasanth                    24    kerala\nann                         antony                      24    usa\nsruthy                      abc                         25    ekm\nnew                         abc                         NULL  NULL\n<\/code><\/pre>\n\n\n\n<p><strong>RIGHT JOIN<\/strong><\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;: gives all selected rows in TableB, plus any common selected rows in TableA.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT table1.column1, table2.column2...\n  FROM table1\n RIGHT JOIN table2\n    ON table1.common_field = table2.common_field;\n<\/code><\/pre>\n\n\n\n<p>Apply it in our sample table :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place\n  FROM TableA\n RIGHT JOIN TableB\n    ON TableA.id = TableB.id2;\n<\/code><\/pre>\n\n\n\n<p>Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>firstName                   lastName                    age     Place\n...............................................................................\narun                        prasanth                    24     kerala\nann                         antony                      24     usa\nsruthy                      abc                         25     ekm\nNULL                        NULL                        24     chennai\n<\/code><\/pre>\n\n\n\n<p><strong>FULL JOIN<\/strong><\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;: returns all selected values from both tables.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT table1.column1, table2.column2...\n  FROM table1\n  FULL JOIN table2\n    ON table1.common_field = table2.common_field;\n<\/code><\/pre>\n\n\n\n<p>Apply it in our sample table :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place\n  FROM TableA\n  FULL JOIN TableB\n    ON TableA.id = TableB.id2;\n<\/code><\/pre>\n\n\n\n<p>Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>firstName                   lastName                    age    Place\n...............................................................................\narun                        prasanth                    24    kerala\nann                         antony                      24    usa\nsruthy                      abc                         25    ekm\nnew                         abc                         NULL  NULL\nNULL                        NULL                        24    chennai\n<\/code><\/pre>\n\n\n\n<p><strong>Interesting Fact<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For INNER joins the order doesn&#8217;t matter.<\/li><li>For (LEFT, RIGHT or FULL) OUTER joins, the order may matter.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Ref: https:\/\/stackoverflow.com\/questions\/5706437\/whats-the-difference-between-inner-join-left-join-right-join-and-full-join<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are different types of joins available in SQL: INNER JOIN: returns rows when there is a match in both tables. LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table. RIGHT JOIN: returns all rows from the right table, even if there are no matches <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3996\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,621,625],"tags":[1105,1101,1106,1102,1104],"class_list":["post-3996","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-power-bi","category-sql","tag-full-join","tag-ifference-between-inner-join","tag-inner-join","tag-left-join","tag-right-join"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3996"}],"version-history":[{"count":1,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3996\/revisions"}],"predecessor-version":[{"id":3997,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3996\/revisions\/3997"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}