{"id":3862,"date":"2024-04-08T15:46:03","date_gmt":"2024-04-08T08:46:03","guid":{"rendered":"https:\/\/www.sqlservertutorial.net\/?page_id=3862"},"modified":"2024-04-09T09:41:18","modified_gmt":"2024-04-09T02:41:18","slug":"sql-server-cross-apply","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/","title":{"rendered":"SQL Server CROSS APPLY"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL Server <code>CROSS APPLY<\/code> clause to perform an inner join a table with a table-valued function or a correlated subquery.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-sql-server-cross-apply-clause'>Introduction to the SQL Server CROSS APPLY clause <a href=\"#introduction-to-the-sql-server-cross-apply-clause\" class=\"anchor\" id=\"introduction-to-the-sql-server-cross-apply-clause\" title=\"Anchor for Introduction to the SQL Server CROSS APPLY clause\">#<\/a><\/h2>\n\n\n\n<p>The <code>CROSS APPLY<\/code> clause allows you to perform an inner join a table with a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-valued-functions\/\">table-valued function<\/a> or a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-correlated-subquery\/\">correlated subquery<\/a>.<\/p>\n\n\n\n<p class=\"note\">In SQL Server, a table-valued function is a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/\">user-defined function<\/a> that <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-valued-functions\/\">returns multiple rows as a table<\/a>.<\/p>\n\n\n\n<p>The <code>CROSS APPLY<\/code> clause works like an <code>INNER JOIN<\/code> clause. But instead of joining two tables, the <code>CROSS APPLY<\/code> clause joins a table with a table-valued function or a correlated subquery.<\/p>\n\n\n\n<p>Here&#8217;s the basic syntax of the <code>CROSS APPLY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  select_list\n<span class=\"hljs-keyword\">FROM<\/span>\n  table1\n  <span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">APPLY<\/span> table_function(table1.column) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">alias<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>table1<\/code> is the main table from which you want to join.<\/li>\n\n\n\n<li><code>table_function<\/code>: is the table-valued function to apply to each row. Alternatively, you can use a correlated subquery.<\/li>\n\n\n\n<li><code>column<\/code>: is the column from <code>table1<\/code> that will be passed as a parameter to the <code>table_function<\/code>.<\/li>\n\n\n\n<li><code>alias<\/code> is the alias for the result set returned by the <code>table_function<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>The <code>CROSS APPLY<\/code> clause will apply the <code>table_function<\/code> to each row from the <code>table1<\/code>. If you use a correlated subquery, the <code>CROSS APPLY<\/code> clause will execute it for each row from the <code>table1<\/code>.<\/p>\n\n\n\n<p>In practice, you should use the <code>CROSS APPLY<\/code> clauses when you cannot use <code>INNER JOIN<\/code> clauses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-cross-apply-clause-examples'>SQL Server CROSS APPLY clause examples <a href=\"#sql-server-cross-apply-clause-examples\" class=\"anchor\" id=\"sql-server-cross-apply-clause-examples\" title=\"Anchor for SQL Server CROSS APPLY clause examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s explore some useful use cases of the <code>CROSS APPLY<\/code> clause.<\/p>\n\n\n\n<p>We&#8217;ll use the <code>production.categories<\/code> and <code>production.products<\/code> tables from the <a href=\"https:\/\/www.sqlservertutorial.net\/getting-started\/sql-server-sample-database\/\">sample database<\/a> for the demonstration:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"494\" height=\"169\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png\" alt=\"SQL Server CROSS APPLY Operator - Sample table example\" class=\"wp-image-145\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png 494w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories-300x103.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-the-sql-server-cross-apply-clause-to-join-a-table-with-a-correlated-subquery'>1) Using the SQL Server CROSS APPLY clause to join a table with a correlated subquery <a href=\"#1-using-the-sql-server-cross-apply-clause-to-join-a-table-with-a-correlated-subquery\" class=\"anchor\" id=\"1-using-the-sql-server-cross-apply-clause-to-join-a-table-with-a-correlated-subquery\" title=\"Anchor for 1) Using the SQL Server CROSS APPLY clause to join a table with a correlated subquery\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>CROSS APPLY<\/code> clause to join the <code>production.categories<\/code> table with a correlated subquery to retrieve the top two most expensive products for each product category:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  c.category_name,\n  r.product_name,\n  r.list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n  production.categories c\n  <span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">APPLY<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span>\n      TOP <span class=\"hljs-number\">2<\/span> *\n    <span class=\"hljs-keyword\">FROM<\/span>\n      production.products p\n    <span class=\"hljs-keyword\">WHERE<\/span>\n      p.category_id = c.category_id\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n      list_price <span class=\"hljs-keyword\">DESC<\/span>,\n      product_name\n  ) r\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  c.category_name,\n  r.list_price <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">category_name       | product_name                                   | list_price\n<span class=\"hljs-comment\">--------------------+------------------------------------------------+---------<\/span>\nChildren Bicycles   | Electra Straight 8 3i (20-inch) - Boy's - 2017 | 489.99\nChildren Bicycles   | Electra Townie 3i EQ (20-inch) - Boys' - 2017  | 489.99\nComfort Bicycles    | Electra Townie Go! 8i - 2017\/2018              | 2599.99\nComfort Bicycles    | Electra Townie Balloon 7i EQ - 2018            | 899.99\nCruisers Bicycles   | Electra Townie Commute Go! - 2018              | 2999.99\nCruisers Bicycles   | Electra Townie Commute Go! Ladies' - 2018      | 2999.99\nCyclocross Bicycles | Trek Boone 7 Disc - 2018                       | 3999.99\nCyclocross Bicycles | Trek Boone 7 - 2017                            | 3499.99\nElectric Bikes      | Trek Powerfly 8 FS Plus - 2017                 | 4999.99\nElectric Bikes      | Trek Powerfly 7 FS - 2018                      | 4999.99\nMountain Bikes      | Trek Fuel EX 9.8 27.5 Plus - 2017              | 5299.99\nMountain Bikes      | Trek Remedy 9.8 - 2017                         | 5299.99\nRoad Bikes          | Trek Domane SLR 9 Disc - 2018                  | 11999.99\nRoad Bikes          | Trek Domane SLR 8 Disc - 2018                  | 7499.99\n(14 rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>For each row from the <code>production.categories<\/code> table, the <code>CROSS APPLY<\/code> executes the following <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-correlated-subquery\/\">correlated subquery<\/a> to retrieve the top two most expensive products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  TOP <span class=\"hljs-number\">2<\/span> *\n<span class=\"hljs-keyword\">FROM<\/span>\n  production.products p\n<span class=\"hljs-keyword\">WHERE<\/span>\n  p.category_id = c.category_id\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  list_price <span class=\"hljs-keyword\">DESC<\/span>,\n  product_name<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-cross-apply-clause-to-join-a-table-with-a-table-valued-function'>2) Using the CROSS APPLY clause to join a table with a table-valued function <a href=\"#2-using-the-cross-apply-clause-to-join-a-table-with-a-table-valued-function\" class=\"anchor\" id=\"2-using-the-cross-apply-clause-to-join-a-table-with-a-table-valued-function\" title=\"Anchor for 2) Using the CROSS APPLY clause to join a table with a table-valued function\">#<\/a><\/h3>\n\n\n\n<p>First, define a table-valued function that returns the top two most expensive products by category id:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> GetTopProductsByCategory (@category_id <span class=\"hljs-built_in\">INT<\/span>)\n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-keyword\">TABLE<\/span>\n<span class=\"hljs-keyword\">AS<\/span>\n<span class=\"hljs-keyword\">RETURN<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span> TOP <span class=\"hljs-number\">2<\/span> *\n    <span class=\"hljs-keyword\">FROM<\/span> production.products p\n    <span class=\"hljs-keyword\">WHERE<\/span> p.category_id = @category_id \n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> list_price <span class=\"hljs-keyword\">DESC<\/span>, product_name\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, use the <code>CROSS APPLY<\/code> clause with the table-valued function <code>GetTopProductsByCategory<\/code> to retrieve the top two most expensive products within each category:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  c.category_name,\n  r.product_name,\n  r.list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n  production.categories c\n  <span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">APPLY<\/span> GetTopProductsByCategory(c.category_id) r\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  c.category_name,\n  r.list_price <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returns the same result as the query that uses the correlated subquery above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='3-using-the-cross-apply-clause-to-process-json-data'>3) Using the CROSS APPLY clause to process JSON data <a href=\"#3-using-the-cross-apply-clause-to-process-json-data\" class=\"anchor\" id=\"3-using-the-cross-apply-clause-to-process-json-data\" title=\"Anchor for 3) Using the CROSS APPLY clause to process JSON data\">#<\/a><\/h3>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-create-table\/\">create a table<\/a> called <code>product_json<\/code> to store the product data:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> product_json(\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    info <span class=\"hljs-keyword\">NVARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>product_json<\/code> table:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>id<\/code> is the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-primary-key\/\">primary key<\/a> column with the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-identity\/\">identity<\/a> attribute.<\/li>\n\n\n\n<li><code>info<\/code> is the <code>NVARCHAR(MAX)<\/code> that will store the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-json\/\">JSON data<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>Second, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-insert-multiple-rows\/\">insert rows<\/a> into the <code>product_json<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> product_json(info)\n<span class=\"hljs-keyword\">VALUES<\/span> \n    (<span class=\"hljs-string\">'{\"Name\": \"Laptop\", \"Price\": 999, \"Category\": \"Electronics\"}'<\/span>),\n    (<span class=\"hljs-string\">'{\"Name\": \"Headphones\", \"Price\": 99, \"Category\": \"Electronics\"}'<\/span>),\n    (<span class=\"hljs-string\">'{\"Name\": \"Book\", \"Price\": 15, \"Category\": \"Books\"}'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, extract information from the info JSON data using the <code>CROSS APPLY<\/code> clause with the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-openjson\/\">OPENJSON()<\/a><\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  p.id,\n  j.*\n<span class=\"hljs-keyword\">FROM<\/span>\n  product_json p\n  <span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">APPLY<\/span> OPENJSON (p.info) <span class=\"hljs-keyword\">WITH<\/span>\n  (\n    <span class=\"hljs-keyword\">Name<\/span> <span class=\"hljs-keyword\">NVARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>),\n    Price <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>),\n    <span class=\"hljs-keyword\">Category<\/span> <span class=\"hljs-keyword\">NVARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>)\n  ) <span class=\"hljs-keyword\">AS<\/span> j;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">id | Name       | Price  | Category\n<span class=\"hljs-comment\">---+------------+--------+-------------<\/span>\n1  | Laptop     | 999.00 | Electronics\n2  | Headphones | 99.00  | Electronics\n3  | Book       | 15.00  | Books\n(3 rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='4-using-the-cross-apply-clause-to-remove-the-nested-replace-function'>4) Using the CROSS APPLY clause to remove the nested REPLACE() function <a href=\"#4-using-the-cross-apply-clause-to-remove-the-nested-replace-function\" class=\"anchor\" id=\"4-using-the-cross-apply-clause-to-remove-the-nested-replace-function\" title=\"Anchor for 4) Using the CROSS APPLY clause to remove the nested REPLACE() function\">#<\/a><\/h3>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-create-table\/\">create a table<\/a> called <code>companies<\/code> that stores the company names:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> companies(\n   <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n   <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-insert-multiple-rows\/\">insert rows<\/a> into the <code>companies<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  companies (<span class=\"hljs-keyword\">name<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'ABC Corporation'<\/span>),\n  (<span class=\"hljs-string\">'XYZ Inc.'<\/span>),\n  (<span class=\"hljs-string\">'JK Pte Ltd'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Suppose you want to get the company names without words like <code>Corporation<\/code>, <code>Inc.<\/code>, and <code>Pte Ltd<\/code>. To achieve this, you can use multiple <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-string-functions\/sql-server-replace-function\/\">REPLACE()<\/a><\/code> functions.<\/p>\n\n\n\n<p>Third, retrieve the company names from the <code>companies<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">TRIM<\/span>(<span class=\"hljs-keyword\">REPLACE<\/span>(<span class=\"hljs-keyword\">REPLACE<\/span>(<span class=\"hljs-keyword\">REPLACE<\/span>(<span class=\"hljs-keyword\">name<\/span>,<span class=\"hljs-string\">'Corporation'<\/span>,<span class=\"hljs-string\">''<\/span>), <span class=\"hljs-string\">'Inc.'<\/span>,<span class=\"hljs-string\">''<\/span>),<span class=\"hljs-string\">'Pte Ltd'<\/span>,<span class=\"hljs-string\">''<\/span>)) company_name\n<span class=\"hljs-keyword\">FROM<\/span> companies;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">company_name\n<span class=\"hljs-comment\">------------<\/span>\nABC\nXYZ\nJK\n(3 rows)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query works as expected but it is quite complex. To fix this, you can utilize the <code>CROSS APPLY<\/code> clause follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">TRIM<\/span>(r3.name) company_name\n<span class=\"hljs-keyword\">FROM<\/span> companies c\n<span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">APPLY<\/span> (<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">REPLACE<\/span>(c.name,<span class=\"hljs-string\">'Corporation'<\/span>, <span class=\"hljs-string\">''<\/span>) <span class=\"hljs-keyword\">name<\/span>) <span class=\"hljs-keyword\">AS<\/span> r1 \n<span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">APPLY<\/span> (<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">REPLACE<\/span>(r1.name,<span class=\"hljs-string\">'Inc.'<\/span>, <span class=\"hljs-string\">''<\/span>) <span class=\"hljs-keyword\">name<\/span>) <span class=\"hljs-keyword\">AS<\/span> r2\n<span class=\"hljs-keyword\">CROSS<\/span> <span class=\"hljs-keyword\">APPLY<\/span> (<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">REPLACE<\/span>(r2.name,<span class=\"hljs-string\">'Pte Ltd'<\/span>, <span class=\"hljs-string\">''<\/span>) <span class=\"hljs-keyword\">name<\/span>) <span class=\"hljs-keyword\">AS<\/span> r3;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this query, we use a series of <code>CROSS<\/code> <code>APPLY<\/code> clauses to progressively replace specific words (<code>Corporation<\/code>, <code>Inc.<\/code>, and <code>Pte Ltd<\/code>) from the company names.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>CROSS<\/code> <code>APPLY<\/code> clause to perform an inner join a table with the table-valued function or a correlated subquery.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3862\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/\"\n\t\t\t\tdata-post-title=\"SQL Server CROSS APPLY\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3862\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/\"\n\t\t\t\tdata-post-title=\"SQL Server CROSS APPLY\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use the SQL Server CROSS APPLY clause to perform an inner join a table with a table-valued function or a correlated subquery.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":100,"menu_order":27,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3862","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server CROSS APPLY Clause<\/title>\n<meta name=\"description\" content=\"Learn how to use the SQL Server CROSS APPLY clause to perform an inner join a table with a table-valued function or a correlated subquery.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server CROSS APPLY Clause\" \/>\n<meta property=\"og:description\" content=\"Learn how to use the SQL Server CROSS APPLY clause to perform an inner join a table with a table-valued function or a correlated subquery.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-09T02:41:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/\",\"name\":\"SQL Server CROSS APPLY Clause\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/products-categories.png\",\"datePublished\":\"2024-04-08T08:46:03+00:00\",\"dateModified\":\"2024-04-09T02:41:18+00:00\",\"description\":\"Learn how to use the SQL Server CROSS APPLY clause to perform an inner join a table with a table-valued function or a correlated subquery.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/products-categories.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/products-categories.png\",\"width\":494,\"height\":169},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-cross-apply\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Basics\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server CROSS APPLY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server CROSS APPLY Clause","description":"Learn how to use the SQL Server CROSS APPLY clause to perform an inner join a table with a table-valued function or a correlated subquery.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server CROSS APPLY Clause","og_description":"Learn how to use the SQL Server CROSS APPLY clause to perform an inner join a table with a table-valued function or a correlated subquery.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2024-04-09T02:41:18+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/","name":"SQL Server CROSS APPLY Clause","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","datePublished":"2024-04-08T08:46:03+00:00","dateModified":"2024-04-09T02:41:18+00:00","description":"Learn how to use the SQL Server CROSS APPLY clause to perform an inner join a table with a table-valued function or a correlated subquery.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","width":494,"height":169},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-cross-apply\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Basics","item":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/"},{"@type":"ListItem","position":3,"name":"SQL Server CROSS APPLY"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=3862"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3862\/revisions"}],"predecessor-version":[{"id":3876,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3862\/revisions\/3876"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/100"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=3862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}