{"id":452,"date":"2023-09-14T09:05:03","date_gmt":"2023-09-13T23:05:03","guid":{"rendered":"http:\/\/www.mrmarkyoung.com\/oracle\/?p=452"},"modified":"2023-10-18T11:01:11","modified_gmt":"2023-10-18T01:01:11","slug":"sql-joins","status":"publish","type":"post","link":"http:\/\/www.mrmarkyoung.com\/oracle\/2023\/09\/14\/sql-joins\/","title":{"rendered":"SQL Joins"},"content":{"rendered":"\n<p>There are four main types of joins in SQL:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>INNER JOIN<\/strong>&nbsp;returns all rows from both tables where there is a match in the join column(s). This is the most common type of join.<\/li>\n\n\n\n<li><strong>LEFT JOIN<\/strong>&nbsp;returns all rows from the left table, and the matching rows from the right table. If there is no match in the right table, the corresponding row in the left table will still be returned.<\/li>\n\n\n\n<li><strong>RIGHT JOIN<\/strong>&nbsp;returns all rows from the right table, and the matching rows from the left table. If there is no match in the left table, the corresponding row in the right table will still be returned.<\/li>\n\n\n\n<li><strong>FULL OUTER JOIN<\/strong>&nbsp;returns all rows from both tables, whether or not there is a match in the join column(s). This is the most complex type of join.<\/li>\n<\/ul>\n\n\n\n<p>In addition to these four main types of joins, there are also a few other types of joins, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CROSS JOIN<\/strong>&nbsp;returns the Cartesian product of two tables, which is the set of all possible combinations of rows from the two tables.<\/li>\n\n\n\n<li><strong>NATURAL JOIN<\/strong>&nbsp;is a special type of inner join that is used when the join columns have the same name and data type in both tables.<\/li>\n<\/ul>\n\n\n\n<p>The type of join that you use will depend on the specific requirements of your query. For example, if you want to find all customers who have placed orders, you would use an inner join. If you want to find all customers, even if they have not placed any orders, you would use a left join.<\/p>\n\n\n\n<p>Here is an example of an inner join:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customer_name, order_id\nFROM customers\nINNER JOIN orders\nON customers.customer_id = orders.customer_id;\n<\/code><\/pre>\n\n\n\n<p>This query will return all rows from the <code>customers<\/code> table where there is a matching row in the <code>orders<\/code> table. The <code>customer_name<\/code> and <code>order_id<\/code> columns will be returned from both tables.<\/p>\n\n\n\n<p>Here is an example of a left join:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customer_name, order_id\nFROM customers\nLEFT JOIN orders\nON customers.customer_id = orders.customer_id;\n<\/code><\/pre>\n\n\n\n<p>This query will return all rows from the <code>customers<\/code> table, whether or not there is a matching row in the <code>orders<\/code> table. The <code>customer_name<\/code> column will be returned from the <code>customers<\/code> table, and the <code>order_id<\/code> column will be returned from the <code>orders<\/code> table if there is a match, or NULL if there is no match.<\/p>\n\n\n\n<p>Here is an example of a FULL OUTER JOIN:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customers.customer_id, customers.name, orders.order_id, orders.order_date\nFROM customers\nFULL OUTER JOIN orders\nON customers.customer_id = orders.customer_id;\n<\/code><\/pre>\n\n\n\n<p>This query will return all rows from the <code>customers<\/code> table, and all rows from the <code>orders<\/code> table, whether or not there is a match in the <code>customer_id<\/code> column. The <code>customer_id<\/code>, <code>name<\/code>, <code>order_id<\/code>, and <code>order_date<\/code> columns will be returned from both tables.<\/p>\n\n\n\n<p>For example, if there is a customer in the <code>customers<\/code> table who has not placed any orders, the customer&#8217;s row will still be returned in the result set, even though there is no matching row in the <code>orders<\/code> table. Similarly, if there is an order in the <code>orders<\/code> table that does not have a matching customer in the <code>customers<\/code> table, the order&#8217;s row will still be returned in the result set, with NULL values for the <code>customer_id<\/code> and <code>name<\/code> columns.<\/p>\n\n\n\n<p>The FULL OUTER JOIN is the most complex type of join, but it is also the most versatile. It can be used to find all rows from both tables, even if there are no matches between the tables. This can be useful for finding all customers, even if they have not placed any orders, or for finding all orders, even if they do not have a matching customer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are four main types of joins in SQL: In addition to these four main types of joins, there are also a few other types of joins, such as: The type of join that you use will depend on the specific requirements of your query. For example, if you want to find all customers who &#8230; <a title=\"SQL Joins\" class=\"read-more\" href=\"http:\/\/www.mrmarkyoung.com\/oracle\/2023\/09\/14\/sql-joins\/\" aria-label=\"Read more about SQL Joins\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71,47,4],"tags":[],"class_list":["post-452","post","type-post","status-publish","format-standard","hentry","category-ms-sql","category-mysql","category-oracle-database"],"_links":{"self":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":2,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"predecessor-version":[{"id":454,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/452\/revisions\/454"}],"wp:attachment":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}