{"id":219,"date":"2008-01-25T17:30:26","date_gmt":"2008-01-25T16:30:26","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=219"},"modified":"2008-01-25T17:30:26","modified_gmt":"2008-01-25T16:30:26","slug":"with-clause","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=219","title":{"rendered":"WITH Clause"},"content":{"rendered":"<p>During my work for <a title=\"AMIS Technology blog\" href=\"http:\/\/technology.amis.nl\/blog\/\" target=\"_blank\">AMIS<\/a> I came across a (to me at least) new feature of the SQL engine. Instead of building an inline view in the from clause of a statement you can build this &#8216;inline view&#8217; before the SQL statement and use the results in the from <a href=\"http:\/\/bar-solutions.com\/wordpress\/wp-content\/9ifinal.gif\"><img loading=\"lazy\" decoding=\"async\" style=\"border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"129\" alt=\"9ifinal\" src=\"http:\/\/bar-solutions.com\/wordpress\/wp-content\/9ifinal-thumb.gif\" width=\"77\" align=\"left\" border=\"0\" \/><\/a>clause. The syntax is pretty straightforward:     <\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:f4f18af2-5fa2-417f-b040-e63d7dc7443b\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;;overflow: none;\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">WITH<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">&lt;<\/span><span style=\"color: #000000; \">your_alias<\/span><span style=\"color: #808080; \">&gt;<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">AS<\/span><span style=\"color: #000000; \"> (<\/span><span style=\"color: #808080; \">&lt;<\/span><span style=\"color: #000000; \">select_statement<\/span><span style=\"color: #808080; \">&gt;<\/span><span style=\"color: #000000; \">) \n<\/span><span style=\"color: #0000FF; \">SELECT<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">&lt;<\/span><span style=\"color: #000000; \">fieldlist<\/span><span style=\"color: #808080; \">&gt;<\/span><span style=\"color: #000000; \"> \n  <\/span><span style=\"color: #0000FF; \">FROM<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">&lt;<\/span><span style=\"color: #000000; \">tables,<\/span><span style=\"color: #808080; \">&gt;&lt;<\/span><span style=\"color: #000000; \">your_alias<\/span><span style=\"color: #808080; \">&gt;<\/span><span style=\"color: #000000; \"> \n <\/span><span style=\"color: #0000FF; \">WHERE<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">&lt;<\/span><span style=\"color: #000000; \">where_clause<\/span><span style=\"color: #808080; \">&gt;<\/span><span style=\"color: #000000; \">;\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>It was quite puzzling to me at first, but I can definitely see possibilities here. Not only is this approach probably more readable than using inline views, but according to Oracle this approach is also much faster. <\/p>\n<p><!--more--><\/p>\n<p>&#160;<a title=\"Improving Query Performance with the SQL WITH Clause\" href=\"http:\/\/www.oracle.com\/technology\/products\/oracle9i\/daily\/oct10.html\" target=\"_blank\" rel=\"nofollow\">Improving Query Performance with the SQL WITH Clause<\/a> tells me a couple of things. First of all, this feature was introduced in Oracle 9<em>i<\/em> (which makes is apparent that I didn&#8217;t know of this feature, because 1) I didn&#8217;t need it before and 2) my major customers at my previous employer were still running Oracle 8<em>i<\/em>). Second, this feature is supposed to speed up query execution. If for instance you have multiple references to the same query, you can of course copy and paste it&#8217;s contents in the query, but that is not really Single Point Of Definition. And the query has to be parsed and executed multiple times where the result should be the same at all times. Of course you can circumvent this issue by creating a real view in the database, but there are times when you don&#8217;t want that, because you eiter don&#8217;t want to expose the query logic, or you don&#8217;t have the rights to do this.<\/p>\n<p>Consider the following query (borrowed from <a title=\"the WITH clause\" href=\"http:\/\/searchoracle.techtarget.com\/expert\/KnowledgebaseAnswer\/0,289625,sid41_gci1213779,00.html\" target=\"_blank\" rel=\"nofollow\">Ask The Oracle Expert<\/a>):<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:fb2c63e2-95b0-4fe6-81a4-5f2ec24ae343\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> product_id\n     , supplier\n     , price\n     , iteminfo\n  <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> (\n       <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table1\n       <\/span><span style=\"color: #0000FF; \">union<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">all<\/span><span style=\"color: #000000; \">\n       <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table2\n       <\/span><span style=\"color: #0000FF; \">union<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">all<\/span><span style=\"color: #000000; \">\n       <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table3\n       ) <\/span><span style=\"color: #0000FF; \">as<\/span><span style=\"color: #000000; \"> T\n <\/span><span style=\"color: #0000FF; \">where<\/span><span style=\"color: #000000; \"> price <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \">\n       ( <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #FF00FF; \">min<\/span><span style=\"color: #000000; \">(price)\n           <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> (\n                <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table1\n                <\/span><span style=\"color: #0000FF; \">union<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">all<\/span><span style=\"color: #000000; \">\n                <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table2\n                <\/span><span style=\"color: #0000FF; \">union<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">all<\/span><span style=\"color: #000000; \">\n                <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table3\n                ) <\/span><span style=\"color: #0000FF; \">as<\/span><span style=\"color: #000000; \"> T1\n          <\/span><span style=\"color: #0000FF; \">where<\/span><span style=\"color: #000000; \"> product_id <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \"> T.product_id )<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>&#160;&#160;&#160; <br \/>and consider the same query, but now using the WITH clause: <\/p>\n<p><\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:f35cd0ea-3948-49bd-b70b-bafc9dd0f47f\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;;font-family:Microsoft Sans Serif;font-size:12\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #0000FF; \">with<\/span><span style=\"color: #000000; \"> simpletable <\/span><span style=\"color: #0000FF; \">as<\/span><span style=\"color: #000000; \">\n(\n<\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table1                 \n<\/span><span style=\"color: #0000FF; \">union<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">all<\/span><span style=\"color: #000000; \">                              \n<\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table2                 \n<\/span><span style=\"color: #0000FF; \">union<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">all<\/span><span style=\"color: #000000; \">                              \n<\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> ... <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> table3                 \n)\n<\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> product_id\n     , supplier\n     , price\n     , iteminfo\n  <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> simpletable <\/span><span style=\"color: #0000FF; \">as<\/span><span style=\"color: #000000; \"> T\n <\/span><span style=\"color: #0000FF; \">where<\/span><span style=\"color: #000000; \"> price <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \">\n       ( <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #FF00FF; \">min<\/span><span style=\"color: #000000; \">(price)\n           <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> simpletable\n          <\/span><span style=\"color: #0000FF; \">where<\/span><span style=\"color: #000000; \"> product_id <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \"> T.product_id )<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>The second query is not only more readable, but it also implements <a title=\"Three Tips Most Excellent for PL\/SQL Developers\" href=\"http:\/\/www.toadworld.com\/Community\/ExpertsBlog\/tabid\/67\/EntryID\/31\/Default.aspx\" target=\"_blank\" rel=\"nofollow\">SPOD<\/a>. If I need to change the &#8216;inline view&#8217;, then I should make sure I make the same changes in both (or maybe even more) queries.<\/p>\n<p>According to Don Burleson in his article <a title=\"Oracle SQL-99 WITH clause\" href=\"http:\/\/www.dba-oracle.com\/t_sql99_with_clause.htm\" target=\"_blank\" rel=\"nofollow\">Oracle SQL-99 WITH clause<\/a> the following apply:<\/p>\n<ul>\n<li>The SQL &#8220;WITH clause&#8221; only works on Oracle 9i release 2 and beyond. <\/li>\n<li>Formally, the &#8220;WITH clause&#8221; is called subquery factoring <\/li>\n<li>The SQL &#8220;WITH clause&#8221; is used when a subquery is executed multiple times <\/li>\n<li>Also useful for recursive queries (SQL-99, but not Oracle SQL) <\/li>\n<\/ul>\n<p>I think it makes the queries more readable as well as it may speed up the execution. It is fun to lean a new feature that has been around for a couple of years \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During my work for AMIS I came across a (to me at least) new feature of the SQL engine. Instead of building an inline view in the from clause of a statement you can build this &#8216;inline view&#8217; before the SQL statement and use the results in the from clause. The syntax is pretty straightforward: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,9],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-oracle","category-sql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=219"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}