{"id":1887,"date":"2019-01-15T12:00:00","date_gmt":"2019-01-15T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/all-about-sql-joins-simple-explanation\/"},"modified":"2026-07-05T03:22:11","modified_gmt":"2026-07-05T01:22:11","slug":"all-about-sql-joins-simple-explanation","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/all-about-sql-joins-simple-explanation\/","title":{"rendered":"All About SQL Joins (Simple Explanation)"},"content":{"rendered":"<p>I would would cover everything you need to know about SQL Joins in this lesson. And you will see it&#8217;s quite clear.<\/p>\n<p>A JOIN in SQL is used to combine record from two or more tables. The combinations is based on a given criteria. Also note that the two tables need be related in some way.<\/p>\n<p>We would be working with the following tables.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Customers Table: Contains list of customers<\/strong><\/p>\n<p>[table id=3 \/]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Orders Table: Contains orders placed by the customers<\/strong><\/p>\n<p>[table id=4 \/]<\/p>\n<p>&nbsp;<\/p>\n<p>We would cover :<\/p>\n<ol>\n<li><a href=\"#t1\">Inner Join<\/a><\/li>\n<li><a href=\"#t2\">Left Join<\/a><\/li>\n<li><a href=\"#t3\">Right Join<\/a><\/li>\n<li><a href=\"#t4\">Full Join<\/a><\/li>\n<li><a href=\"#t5\">Cross-Join<\/a><\/li>\n<li><a href=\"#t6\">Self Join<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. INNER JOIN<\/strong><\/h4>\n<p>You use INNER JOIN to return records that have matching values in both tables.\u00a0 Note that this is the same as JOIN. Therefore, if you use only JOIN, the it assumes INNER JOIN.<\/p>\n<p>Now copy and execute the code below in SQL Server.\u00a0 You will have the result in Table 1.0.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">SELECT<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.Contact, <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID, o.CustomerID, o.OrderDetails  \n<span style=\"color: #008800; font-weight: bold;\">FROM<\/span> Customers <span style=\"color: #008800; font-weight: bold;\">c<\/span> <span style=\"color: #008800; font-weight: bold;\">INNER<\/span> <span style=\"color: #008800; font-weight: bold;\">JOIN<\/span> Orders o\n<span style=\"color: #008800; font-weight: bold;\">ON<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID <span style=\"color: #333333;\">=<\/span> o.CustomerID\n<\/pre>\n<\/div>\n<p>Listing 1.0: INNER JOIN (JOIN)<\/p>\n<p>[table id=5 \/]<br \/>\nTable 1.0: INNER JOIN (JOIN)<\/p>\n<p>You should note in Table 1.0, that only the records where there is matching CustomerID is returned. I would recommend you try this yourself. You can get the excel table from here. Then see how to import excel to MSSQL from here.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. LEFT JOIN<\/strong><\/h4>\n<p>Use LEFT JOIN to return all records in the left table plus matching records in the right table. Null is used in place of missing values in the right table. Note that LEFT JOIN is the same as LEFT OUTER JOIN.<\/p>\n<p>I have written the query. You can copy and paste the code below in SQL Server. Then run it. You will have the result shown in Table 1.1<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">SELECT<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.Contact, <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID, o.CustomerID, o.OrderDetails  \n<span style=\"color: #008800; font-weight: bold;\">FROM<\/span> Customers <span style=\"color: #008800; font-weight: bold;\">c<\/span> <span style=\"color: #008800; font-weight: bold;\">LEFT<\/span> <span style=\"color: #008800; font-weight: bold;\">JOIN<\/span> Orders o\n<span style=\"color: #008800; font-weight: bold;\">ON<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID <span style=\"color: #333333;\">=<\/span> o.CustomerID\n<\/pre>\n<\/div>\n<p>Listin 1.1: SQL Query for LEFT JOIN (LEFT OUTER JOIN)<\/p>\n<p>[table id=6 \/]<\/p>\n<p>Table 1.1: Table for LEFT JOIN(LEFT OUTER)<\/p>\n<p>We see that in Table 1.1, all the records for Customers Table (Left Table) is included. Furthermore, for customers with no orders, NULL is used.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. RIGHT JOIN<\/strong><\/h4>\n<p>You use RIGHT JOIN to return all records from the right table table plus matching records in the left table. It is same as RIGHT OUTER JOIN.\u00a0 Null is used in place of missing values in the right table. I have written the query for you. If we run the code below, then we would have the result in Table 1.3. You can view RIGHT JOIN as opposite of LEFT JOIN. I recommend you try it yourself.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">SELECT<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.Contact, <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID, o.CustomerID, o.OrderDetails  \n<span style=\"color: #008800; font-weight: bold;\">FROM<\/span> Customers <span style=\"color: #008800; font-weight: bold;\">c<\/span> <span style=\"color: #008800; font-weight: bold;\">RIGHT<\/span> <span style=\"color: #008800; font-weight: bold;\">JOIN<\/span> Orders o\n<span style=\"color: #008800; font-weight: bold;\">ON<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID <span style=\"color: #333333;\">=<\/span> o.CustomerID\n<\/pre>\n<\/div>\n<p>Listing 1.3: SQL Query for RIGHT JOIN (RIGHT OUTER JOIN)<\/p>\n<p>You can see in Table 1.3 that all the records for the Orders table (right table) is included.<\/p>\n<p>&nbsp;<\/p>\n<p>[table id=7 \/]<\/p>\n<p>Table 1.3: Result of RIGHT JOIN (RIGHT OUTER JOIN)<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. FULL JOIN<\/strong><\/h4>\n<p>We use FULL JOIN to return all records from both tables. You can view this as a combination of LEFT JOIN\u00a0 and RIGHT JOIN.\u00a0 I have written the query as shown below. I also recommend you try it yourself.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">SELECT<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.Contact, <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID, o.CustomerID, o.OrderDetails  \n<span style=\"color: #008800; font-weight: bold;\">FROM<\/span> Customers <span style=\"color: #008800; font-weight: bold;\">c<\/span> <span style=\"color: #008800; font-weight: bold;\">FULL<\/span> <span style=\"color: #008800; font-weight: bold;\">JOIN<\/span> Orders o\n<span style=\"color: #008800; font-weight: bold;\">ON<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID <span style=\"color: #333333;\">=<\/span> o.CustomerID\n<\/pre>\n<\/div>\n<p>Listing 1.4: SQL Query FULL JOIN (FULL OUTER JOIN)<\/p>\n<p>[table id=8 \/]<\/p>\n<p>Table 1.4: FULL JOIN<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. CROSS JOIN<\/strong><\/h4>\n<p>You use CROSS JOIN to return a result that is number of rows of first table multiplied by number of rows in second table.\u00a0 We also call this Cartesian Product. For our example, the output would be a 25 rows table!<\/p>\n<p>I would only provide the query for you. So you run it yourself and see the result. You have the SQL query in Listing 1.5. So run it and leave a comment to let me know what you have.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">SELECT<\/span> <span style=\"color: #008800; font-weight: bold;\">c<\/span>.Contact, <span style=\"color: #008800; font-weight: bold;\">c<\/span>.CustomerID, o.CustomerID, o.OrderDetails  \n<span style=\"color: #008800; font-weight: bold;\">FROM<\/span> Customers <span style=\"color: #008800; font-weight: bold;\">c<\/span> <span style=\"color: #008800; font-weight: bold;\">CROSS<\/span> <span style=\"color: #008800; font-weight: bold;\">JOIN<\/span> Orders o\n<\/pre>\n<\/div>\n<p>Listing 1.5: CROSS JOIN (Cartesian Product)<\/p>\n<p>Not that the SQL query for CROSS JOIN does not include the ON keyword<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Self Join<\/strong><\/h4>\n<p>We use to indicate a join of a table with itself. I would provide you with an example of this as shown in Listing 1.6.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">SELECT<\/span> c1.CustomerName, c2.CustomerID \n<span style=\"color: #008800; font-weight: bold;\">FROM<\/span> Customers c1 <span style=\"color: #008800; font-weight: bold;\">JOIN<\/span> Customers c2\n<span style=\"color: #008800; font-weight: bold;\">ON<\/span> c1.CustomerID <span style=\"color: #333333;\">=<\/span> c2.CustomerID\n<\/pre>\n<\/div>\n<p>Listing 1.6: Self Join<\/p>\n<p>The result of this code is given in Table 1.5<\/p>\n<p>[table id=9 \/]<\/p>\n<p>Table 1.5: Self Join<\/p>\n<p>Also note that we don&#8217;t write a &#8216;SELF JOIN&#8217; query.\u00a0 We simple join a table with itself using any of the joins we have discussed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would would cover everything you need to know about SQL Joins in this lesson. And you will see it&#8217;s quite clear. A JOIN in &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1887","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1887"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1887\/revisions"}],"predecessor-version":[{"id":2055,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1887\/revisions\/2055"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}