{"id":453,"date":"2009-02-13T11:21:55","date_gmt":"2009-02-13T16:21:55","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=453"},"modified":"2015-03-31T10:56:41","modified_gmt":"2015-03-31T15:56:41","slug":"ascii-sql-joins","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=453","title":{"rendered":"ASCII SQL Joins"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#INNER_JOIN\">INNER JOIN<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#RIGHT_OUTER_JOIN_ASCII_and_Oracle_9i\">RIGHT OUTER JOIN (ASCII and Oracle 9i)<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#RIGHT_OUTER_JOIN_ASCII_and_Oracle_9i_1\">RIGHT OUTER JOIN (ASCII and Oracle 9i)<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#FULL_OUTER_JOIN_ASCII_and_Oracle_9i\">FULL OUTER JOIN (ASCII and Oracle 9i)<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Pre_Oracle_9i_OUTER_JOIN\">Pre Oracle 9i OUTER JOIN<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Oracle_OUTER_JOIN_Caveats\">Oracle OUTER JOIN Caveats<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#UNION_and_UNION_ALL\">UNION and UNION ALL<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2009\/02\/sql_joins.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2009\/02\/sql_joins-300x234.jpg\" alt=\"sql_joins\" width=\"300\" height=\"234\" class=\"aligncenter size-medium wp-image-10607\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2009\/02\/sql_joins-300x234.jpg 300w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2009\/02\/sql_joins.jpg 641w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/h6><\/span>\n<span id=\"INNER_JOIN\"><h2>INNER JOIN<\/h2><\/span>\n<p>*Returns all rows from both tables where there is a match<br \/>\n*Example<\/p>\n<pre lang=\"sql\">SELECT field1, field2, field3\r\nFROM first_table\r\nINNER JOIN second_table\r\nON first_table.keyf = second_table.foreign_key<\/pre>\n<span id=\"RIGHT_OUTER_JOIN_ASCII_and_Oracle_9i\"><h2>RIGHT OUTER JOIN (ASCII and Oracle 9i)<\/h2><\/span>\n<p>*All rows from the left table (dept) are retained<br \/>\n*Example<\/p>\n<pre lang=\"sql\">\r\nSELECT dept.*, emp.*\r\nFROM dept\r\nLEFT OUTER JOIN emp\r\nON dept.deptno = dept.deptno\r\n<\/pre>\n<p>*Returns all the rows from the first table, even if there are no matches in the second table<br \/>\n*Example<\/p>\n<pre lang=\"sql\">\r\nSELECT field1, field2, field3\r\nFROM first_table\r\nLEFT JOIN second_table\r\nON first_table.key=second_table.foreign_key\r\n<\/pre>\n<span id=\"RIGHT_OUTER_JOIN_ASCII_and_Oracle_9i_1\"><h2>RIGHT OUTER JOIN (ASCII and Oracle 9i)<\/h2><\/span>\n<p>*All rows from the right table (emp) are retained<br \/>\n*Example<\/p>\n<pre lang=\"sql\">\r\nSELECT dept.*, emp.*\r\nFROM dept\r\nRIGHT OUTER JOIN emp\r\nON dept.deptno = dept.deptno\r\n<\/pre>\n<p>*Returns all the rows from the second table, even if there are no matches in the first table.<br \/>\n*Example<\/p>\n<pre lang=\"sql\">\r\nSELECT field1, field2, field3\r\nFROM first_table\r\nRIGHT JOIN second_table\r\nON first_table.key=second_table.foreign_key\r\n<\/pre>\n<span id=\"FULL_OUTER_JOIN_ASCII_and_Oracle_9i\"><h2>FULL OUTER JOIN (ASCII and Oracle 9i)<\/h2><\/span>\n<p>*All rows from both the left and right tables (dept and emp) are retained<br \/>\n*Example<\/p>\n<pre lang=\"sql\">\r\nselect p.part_id, s.supplier_name\r\nfrom part p\r\nFULL OUTER JOIN supplier s\r\nON p.supplier_id = s.supplier_id\r\n<\/pre>\n<span id=\"Pre_Oracle_9i_OUTER_JOIN\"><h2>Pre Oracle 9i OUTER JOIN<\/h2><\/span>\n<p>*Returns rows even if there is not a match from the outer table (return null values)<br \/>\n*Outer table is the table denoted by the outer join sign<br \/>\n*Driving table is the other table without outer join sign<br \/>\n*Null values are returned from outer table if no match found<br \/>\n*Expression<\/p>\n<pre lang=\"sql\">\r\nSELECT field1, field2, field3\r\nFROM left_table left, right_table right\r\nWHERE left.key = right.key (+)\r\nAND right.field1 (+) = \u2018some value\u2019\r\n<\/pre>\n<span id=\"Oracle_OUTER_JOIN_Caveats\"><h2>Oracle OUTER JOIN Caveats<\/h2><\/span>\n<p>*Half baked multi-column joins<\/p>\n<pre lang=\"sql\">\r\nselect * from t1, t2 \r\nwhere t1.c1=t2.c1(+) and t1.c2=t2.c2(+)\r\nIncomplete join trail\r\nselect * from t1,t2,t3,t4 where t1.c1=t2.c1(+)\r\nand t2.c2=t3.c2(+) and t3.c3=t4.c3(+)\r\nJoin to a constant\r\nselect * from t1, t2 where t1.c1=t2.c1(+)\r\nand t1.c2(+)=\u2018Y\u2019\r\n<\/pre>\n<span id=\"UNION_and_UNION_ALL\"><h2>UNION and UNION ALL<\/h2><\/span>\n<p>*Expression<\/p>\n<pre lang=\"sql\">\r\nSQL statement 1\r\nUNION\r\nSQL statement 2\r\nRestrictions\r\n<\/pre>\n<p>*All selected columns need to be of the same data type<br \/>\n*Only selects distinct values<br \/>\n*Use UNION ALL to select all values<\/p>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/www.codeproject.com\/Articles\/33052\/Visual-Representation-of-SQL-Joins\">http:\/\/www.codeproject.com\/Articles\/33052\/Visual-Representation-of-SQL-Joins<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>INNER JOIN *Returns all rows from both tables where there is a match *Example SELECT field1, field2, field3 FROM first_table INNER JOIN second_table ON first_table.keyf = second_table.foreign_key RIGHT OUTER JOIN (ASCII and Oracle 9i) *All rows from the left table &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=453\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[35,47],"tags":[],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-oracle","category-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-7j","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=453"}],"version-history":[{"count":7,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":10609,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions\/10609"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}