Introduction
The APPLY operator allows us to invoke a table-valued function for each row returned by an outer table expression of a query. The APPLY operator allows us to join two table expressions; the right table expression is processed every time for each row from the left table expression.
The left table expression is evaluated first and then the right table expression is evaluated against each row of the left table expression for the final result set. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.
Now we create two tables, the first is Employee and the second Project table.
Employee Table
Now insert some data into the Employee table.
![Employee table]()
Project Table
Insert data into the project table.
![Project table]()
Forms of Apply
SQL Server contains two forms of Apply: CROSS APPLY and OUTER APPLY.
CROSS APPLY in SQL Server
CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. In other words, the result of CROSS APPLY doesn’t contain any row of left side table expression for which no result is obtained from right side table expression. CROSS APPLY for work as a row-by-row INNER JOIN.
INNER JOIN Query
CROSS APPLY Query
Both queries produce the same result.
![CROSS APPLY in SQL Server]()
OUTER APPLY in SQL Server
OUTER APPLY returns both rows that produce a result set, and rows that do not, with NULL values in the columns produced by the table-valued function. OUTER APPLY works as LEFT OUTER JOIN.
LEFT OUTER JOIN Query
OUTER APPLY Query
Above both queries produce the same result.
![OUTER APPLY in SQL Server]()
APPLY with User Define a function
We can perform APPLY operation with a function that may be a scalar or table-valued function. This function will invoke each row and return a result that will be associated with the outer table.
Example 1
Firstly, create a function.
The above function will return a scalar value that is the combined result of the Department column and Project column.
Now we perform APPLY on this function.
Output
![APPLY with User Define function]()
Example 2
Firstly, create a table-valued function.
The above function return information on all project that’s Project_Id is greater than the given Project_id.
Output
![id]()
APPLY with TOP Command
Output
![runAPPLY with TOP Command]()
Above query return top Employee details for each project. We can perform the same operation using APPLY.
The above query produces the same result as the previous query.
Example 3
Output
![APPLY with TOP Command]()
Suppose we have a table that contains State names and City names but city names are stored in comma separate manner, now we want to split each city name. For this, we can use APPLY Method as below.
For this, we create a function that split city name and return a table that contains the list of city names.
Now we use this function to split the city name.
Output
![APPLY with TOP Command]()
Summary
In this article, you will learn about Apply operator in SQL Server and its types with examples. Learn Cross Apply And Outer Apply in SQL Server. Thanks for reading the article.