{"componentChunkName":"component---src-templates-tags-js","path":"/tags/t-sql/","result":{"data":{"allMarkdownRemark":{"totalCount":1,"edges":[{"node":{"id":"d29d5402-6523-5283-973e-76dbdb3d85ba","html":"<p>I was working on a problem and I was desperate to do a clean job and not create a mess that no one can maintain it, and accidentally I read an article about SQL <code class=\"language-text\">Apply</code> operator.</p>\n<!--more-->\n<p>The <code class=\"language-text\">APPLY</code> operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query.</p>\n<p>The table-valued function acts as the right input and the outer table expression acts as the left input. The advantage over inner join is that it is faster and you can handle conditional joins easily using this trick.\r\nA quick reminder on the terms.</p>\n<blockquote>\n<p>INNER JOIN is the most used construct in SQL: it joins two tables together, selecting only those row combinations for which a JOIN condition is true.</p>\n</blockquote>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"87720133737954500000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`SELECT  *\r\nFROM    tblUser\r\nJOIN    tblProfile\r\nON      tblUser.Id = tblProfile.UserId`, `87720133737954500000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"sql\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-sql line-numbers\"><code class=\"language-sql\"><span class=\"token keyword\">SELECT</span>  <span class=\"token operator\">*</span>\r\n<span class=\"token keyword\">FROM</span>    tblUser\r\n<span class=\"token keyword\">JOIN</span>    tblProfile\r\n<span class=\"token keyword\">ON</span>      tblUser<span class=\"token punctuation\">.</span>Id <span class=\"token operator\">=</span> tblProfile<span class=\"token punctuation\">.</span>UserId</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span></span></pre></div>\n<p>But for some tasks the sets are not self-sufficient. For instance, let’s consider the following query:\r\nWe have <code class=\"language-text\">tblUser</code> and <code class=\"language-text\">tblProfile</code>. <code class=\"language-text\">tblProfile</code> has a column called <code class=\"language-text\">rowcount</code>.</p>\n<p>For each row from <code class=\"language-text\">tblUser</code> we need to select first <code class=\"language-text\">rowcount</code> rows from <code class=\"language-text\">tblProfile</code>, ordered by <code class=\"language-text\">tblProfile.Id</code>.</p>\n<p>We cannot come up with a join condition here. The join condition, should it exist, would involve the row number, which is not present in <code class=\"language-text\">tblProfile</code>, and there is no way to calculate a row number only from the values of columns of any given row in <code class=\"language-text\">tblProfile</code>.</p>\n<p>That’s where the <code class=\"language-text\">CROSS APPLY</code> can be used:</p>\n<div\n              class=\"gatsby-code-button-container\"\n              data-toaster-id=\"17025724716065872000\"\n              data-toaster-class=\"gatsby-code-button-toaster\"\n              data-toaster-text-class=\"gatsby-code-button-toaster-text\"\n              data-toaster-text=\"Copied to clipboard ✅\"\n              data-toaster-duration=\"3500\"\n              onClick=\"copyToClipboard(`SELECT _\r\nFROM tblUser\r\nCROSS APPLY\r\n(\r\nSELECT TOP (tblUser.rowcount)\r\nFROM tblProfile\r\nORDER BY\r\nId\r\n) t2`, `17025724716065872000`)\"\n            >\n              <div\n                class=\"gatsby-code-button\"\n                data-tooltip=\"\"\n              >\n                Copy<svg class=\"gatsby-code-button-icon\" xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path fill=\"none\" d=\"M0 0h24v24H0V0z\"/><path d=\"M16 1H2v16h2V3h12V1zm-1 4l6 6v12H6V5h9zm-1 7h5.5L14 6.5V12z\"/></svg>\n              </div>\n            </div>\n<div class=\"gatsby-highlight\" data-language=\"sql\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-sql line-numbers\"><code class=\"language-sql\"><span class=\"token keyword\">SELECT</span> _\r\n<span class=\"token keyword\">FROM</span> tblUser\r\n<span class=\"token keyword\">CROSS</span> <span class=\"token keyword\">APPLY</span>\r\n<span class=\"token punctuation\">(</span>\r\n<span class=\"token keyword\">SELECT</span> <span class=\"token keyword\">TOP</span> <span class=\"token punctuation\">(</span>tblUser<span class=\"token punctuation\">.</span><span class=\"token keyword\">rowcount</span><span class=\"token punctuation\">)</span>\r\n<span class=\"token keyword\">FROM</span> tblProfile\r\n<span class=\"token keyword\">ORDER</span> <span class=\"token keyword\">BY</span>\r\nId\r\n<span class=\"token punctuation\">)</span> t2</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p><strong>Summary:</strong></p>\n<p>While most queries which employ <code class=\"language-text\">CROSS APPLY</code> can be rewritten using an <code class=\"language-text\">INNER JOIN</code>, <code class=\"language-text\">CROSS APPLY</code> can yield better execution plan and better performance, since it can limit the set being joined yet before the join occurs.</p>","timeToRead":1,"frontmatter":{"title":"An awesome SQL operator","unformattedDate":"2016-06-21T00:00:00.000Z","date":"Jun 21, 2016","path":"/an-awesome-sql-operator/","author":"Yaser Adel Mehraban","tags":["cross apply","join","query","t-sql"],"thumbnail":null}}}]}},"pageContext":{"tag":"t-sql"}},"staticQueryHashes":[],"slicesMap":{}}