{"id":245,"date":"2018-12-13T07:22:09","date_gmt":"2018-12-13T07:22:09","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/13\/sql-join\/"},"modified":"2024-03-03T17:55:46","modified_gmt":"2024-03-03T17:55:46","slug":"sql-join","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-join\/","title":{"rendered":"SQL JOIN"},"content":{"rendered":"<p>A SQL JOIN clause is used to combine rows from two or more <a href=\"https:\/\/barkhane.com\/sql\/sql-table\/\">tables<\/a>, based on a related column between them.<\/p>\n<p>Let\\&#8217;s look at a selection from the \\&#8221;Orders\\&#8221; table:<\/p>\n<div class=\"w3-responsive\">\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th>OrderID<\/th>\n<th>CustomerID<\/th>\n<th>OrderDate<\/th>\n<\/tr>\n<tr>\n<td>10308<\/td>\n<td>2<\/td>\n<td>1996-09-18<\/td>\n<\/tr>\n<tr>\n<td>10309<\/td>\n<td>37<\/td>\n<td>1996-09-19<\/td>\n<\/tr>\n<tr>\n<td>10310<\/td>\n<td>77<\/td>\n<td>1996-09-20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Then, look at a selection from the \\&#8221;Customers\\&#8221; table:<\/p>\n<div class=\"w3-responsive\">\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th>CustomerID<\/th>\n<th>CustomerName<\/th>\n<th>ContactName<\/th>\n<th>Country<\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Alfreds Futterkiste<\/td>\n<td>Maria Anders<\/td>\n<td>Germany<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Ana Trujillo Emparedados y helados<\/td>\n<td>Ana Trujillo<\/td>\n<td>Mexico<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Antonio Moreno Taquer\u00eda<\/td>\n<td>Antonio Moreno<\/td>\n<td>Mexico<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Notice that the \\&#8221;CustomerID\\&#8221; column in the \\&#8221;Orders\\&#8221; table refers to the \\&#8221;CustomerID\\&#8221; in the \\&#8221;Customers\\&#8221; table. The relationship between the two tables above is the \\&#8221;CustomerID\\&#8221; column.<\/p>\n<p>Then, we can create the following SQL statement (that contains an INNER JOIN), that selects records that have matching values in both tables:<\/p>\n<div class=\"w3-example\">\n<h3>Example<\/h3>\n<div class=\"w3-code notranslate sqlHigh\">SELECT\u00a0Orders.OrderID, Customers.CustomerName, Orders.OrderDate<br \/>\nFROM\u00a0Orders<br \/>\nINNER\u00a0JOIN\u00a0Customers\u00a0ON\u00a0Orders.CustomerID=Customers.CustomerID;<\/div>\n<\/div>\n<p>and it will produce something like this:<\/p>\n<hr \/>\n<h2>Different Types of SQL JOINs<\/h2>\n<p>Here are the different types of the JOINs in SQL:<\/p>\n<ul>\n<li><b>(INNER) JOIN<\/b>: Returns records that have matching values in both tables<\/li>\n<li><b>LEFT (OUTER) JOIN<\/b>: Return all records from the left table, and the matched records from the right table<\/li>\n<li><b>RIGHT (OUTER) JOIN<\/b>: Return all records from the right table, and the matched records from the left table<\/li>\n<li><b>FULL (OUTER) JOIN<\/b>: Return all records when there is a match in either left or right table<\/li>\n<\/ul>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-248\" src=\"https:\/\/barkhane.com\/sql\/wp-content\/uploads\/sites\/4\/2018\/12\/joins-300x58.jpg\" alt=\"\" width=\"734\" height=\"142\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A SQL JOIN clause is used to combine rows from two or more tables, based on a related column between [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}