Introduction
SQL Server CROSS APPLY clause is used to combine rows from a left-side table with the results returned by a table expression that is evaluated for each row of the left-side table.
Unlike a regular JOIN, CROSS APPLY allows the right-side table expression to reference columns from the left-side table. This makes it especially useful when working with correlated subqueries, table-valued functions, and queries that return different results for each row.
CROSS APPLY behaves similarly to INNER JOIN because it returns only the left-side rows for which the right-side expression produces at least one row.
In this tutorial, you will learn how to use the SQL Server CROSS APPLY operator with practical examples, syntax, correlated subqueries, table-valued functions, TOP, VALUES, and other SQL queries.
SQL Server CROSS APPLY Syntax
The following is the basic syntax of the CROSS APPLY operator in SQL Server:
SELECT
column_name1,
column_name2
FROM
left_table AS A
CROSS APPLY
table_expression AS B;
Syntax explanation:
SELECT: Specifies the columns to retrieve.FROM: Defines the left-side table.CROSS APPLY: Evaluates the right-side table expression for each row from the left-side table.left_table: The table that provides the input rows.table_expression: A subquery or table-valued function that can reference columns from the left table.AandB: Table aliases used to identify the two sources.
The most important feature of CROSS APPLY is that the right-side expression can use values from the current row of the left-side table.
SQL Server CROSS APPLY Example
Let’s understand how CROSS APPLY works with a practical example.
Suppose we have two tables: Departments and Employees.
Departments Table
| DepartmentID | DepartmentName |
|---|---|
| 101 | IT |
| 102 | HR |
| 103 | Finance |
Employees Table
| EmployeeID | EmployeeName | DepartmentID | Salary |
|---|---|---|---|
| 1 | John | 101 | 50000 |
| 2 | David | 101 | 60000 |
| 3 | Sarah | 102 | 45000 |
| 4 | Michael | 102 | 55000 |
Now, let’s use CROSS APPLY to retrieve employees for each department.
SELECT
d.DepartmentID,
d.DepartmentName,
e.EmployeeName,
e.Salary
FROM Departments AS d
CROSS APPLY
(
SELECT
EmployeeName,
Salary
FROM Employees
WHERE Employees.DepartmentID = d.DepartmentID
) AS e;
Output:
| DepartmentID | DepartmentName | EmployeeName | Salary |
|---|---|---|---|
| 101 | IT | John | 50000 |
| 101 | IT | David | 60000 |
| 102 | HR | Sarah | 45000 |
| 102 | HR | Michael | 55000 |
Explanation:
drepresents the Departments table.erepresents the result of the subquery.- The subquery uses
d.DepartmentIDto find matching employees. - SQL Server evaluates the right-side expression for each department.
- Departments without matching employees are excluded from the result.
This example demonstrates how CROSS APPLY retrieves related records using a correlated table expression.
SQL Server CROSS APPLY with Correlated Subquery
A correlated subquery is a query that references columns from an outer query. CROSS APPLY is particularly useful when you want to return multiple columns or rows from a correlated table expression.
For example, the following query retrieves employees whose salary is greater than 50000 in each department.
SELECT
d.DepartmentName,
e.EmployeeName,
e.Salary
FROM Departments AS d
CROSS APPLY
(
SELECT
EmployeeName,
Salary
FROM Employees
WHERE DepartmentID = d.DepartmentID
AND Salary > 50000
) AS e;
Explanation:
- The outer query reads each department.
- The subquery references the current DepartmentID.
- Only employees with salaries greater than 50000 are returned.
- Departments without qualifying employees are excluded.
This technique is useful when each row from the left table needs a different filtered result.
SQL Server CROSS APPLY with TOP
You can use CROSS APPLY with the TOP clause to retrieve a limited number of related records for each row in the left-side table.
For example, the following query retrieves the highest-paid employee from each department.
SELECT
d.DepartmentName,
e.EmployeeName,
e.Salary
FROM Departments AS d
CROSS APPLY
(
SELECT TOP (1)
EmployeeName,
Salary
FROM Employees
WHERE DepartmentID = d.DepartmentID
ORDER BY Salary DESC
) AS e;
Output:
| DepartmentName | EmployeeName | Salary |
|---|---|---|
| IT | David | 60000 |
| HR | Michael | 55000 |
Explanation:
TOP (1)returns one employee for each department.- The WHERE condition matches employees with the current department.
ORDER BY Salary DESCselects the highest salary.- CROSS APPLY returns the result for departments where a matching employee exists.
You can also retrieve the two highest-paid employees from each department by changing TOP (1) to TOP (2).
SELECT
d.DepartmentName,
e.EmployeeName,
e.Salary
FROM Departments AS d
CROSS APPLY
(
SELECT TOP (2)
EmployeeName,
Salary
FROM Employees
WHERE DepartmentID = d.DepartmentID
ORDER BY Salary DESC
) AS e;
This approach is useful for reports that need the highest-paid employees, most recent transactions, or top products within each category.
SQL Server CROSS APPLY with Table-Valued Function
CROSS APPLY is commonly used with table-valued functions (TVFs). A table-valued function returns a table result that can be used in the FROM clause.
Suppose we create an inline table-valued function that returns employees for a specific department.
CREATE FUNCTION dbo.GetEmployeesByDepartment
(
@DepartmentID INT
)
RETURNS TABLE
AS
RETURN
(
SELECT
EmployeeID,
EmployeeName,
Salary
FROM Employees
WHERE DepartmentID = @DepartmentID
);
Now, use CROSS APPLY to execute the function for each department.
SELECT
d.DepartmentName,
e.EmployeeID,
e.EmployeeName,
e.Salary
FROM Departments AS d
CROSS APPLY dbo.GetEmployeesByDepartment(d.DepartmentID) AS e;
Explanation:
dbo.GetEmployeesByDepartmentis a table-valued function.- The function receives DepartmentID from the current department row.
- It returns employees belonging to that department.
- CROSS APPLY combines the function result with the department information.
- Departments without matching employees are excluded.
This approach is useful when you want to reuse table-returning logic across multiple SQL queries.
SQL Server CROSS APPLY with VALUES
SQL Server supports table value constructors using the VALUES clause. You can combine VALUES with CROSS APPLY to create calculated rows based on each row of the left-side table.
For example, suppose we want to display different salary values for each employee.
SELECT
e.EmployeeName,
v.SalaryType,
v.SalaryAmount
FROM Employees AS e
CROSS APPLY
(
VALUES
('Current Salary', e.Salary),
('Annual Salary', e.Salary * 12)
) AS v(SalaryType, SalaryAmount);
Output:
| EmployeeName | SalaryType | SalaryAmount |
|---|---|---|
| John | Current Salary | 50000 |
| John | Annual Salary | 600000 |
| David | Current Salary | 60000 |
| David | Annual Salary | 720000 |
| Sarah | Current Salary | 45000 |
| Sarah | Annual Salary | 540000 |
| Michael | Current Salary | 55000 |
| Michael | Annual Salary | 660000 |
Explanation:
- The VALUES constructor creates two rows for each employee.
- The first row contains the current salary.
- The second row calculates the annual salary.
- CROSS APPLY combines these calculated rows with the employee information.
This technique is useful for unpivoting values, creating calculated columns as rows, and generating multiple related records.
SQL Server CROSS APPLY with WHERE Clause
You can use the WHERE clause with CROSS APPLY to filter the final result.
For example, the following query retrieves employees whose salary is greater than 50000.
SELECT
d.DepartmentName,
e.EmployeeName,
e.Salary
FROM Departments AS d
CROSS APPLY
(
SELECT
EmployeeName,
Salary
FROM Employees
WHERE DepartmentID = d.DepartmentID
) AS e
WHERE e.Salary > 50000;
Explanation:
- CROSS APPLY retrieves matching employees for each department.
- The WHERE clause filters the resulting employee records.
- Only employees with salaries greater than 50000 appear in the output.
You can also place filtering conditions inside the APPLY subquery when you want to filter each row’s related result directly.
SQL Server CROSS APPLY with ORDER BY
You can use ORDER BY inside the right-side expression when it is combined with TOP or another construct that allows ordering.
For example, the following query retrieves the two most recent orders for each customer.
Suppose we have a Customers table and an Orders table.
SELECT
c.CustomerID,
c.CustomerName,
o.OrderID,
o.OrderDate,
o.TotalAmount
FROM Customers AS c
CROSS APPLY
(
SELECT TOP (2)
OrderID,
OrderDate,
TotalAmount
FROM Orders
WHERE CustomerID = c.CustomerID
ORDER BY OrderDate DESC
) AS o;
Explanation:
- The query processes each customer.
- The subquery finds orders belonging to the current customer.
TOP (2)limits the result to two orders.ORDER BY OrderDate DESCselects the most recent orders.- Customers without orders are excluded.
This query is useful for customer activity reports and recent transaction summaries.
SQL Server CROSS APPLY with Aggregate Functions
You can use aggregate functions inside a CROSS APPLY expression to calculate information for each row of the left-side table.
For example, the following query calculates the total salary of employees in each department.
SELECT
d.DepartmentName,
a.TotalEmployees,
a.AverageSalary,
a.TotalSalary
FROM Departments AS d
CROSS APPLY
(
SELECT
COUNT(*) AS TotalEmployees,
AVG(Salary) AS AverageSalary,
SUM(Salary) AS TotalSalary
FROM Employees
WHERE DepartmentID = d.DepartmentID
) AS a;
Because this subquery uses aggregate functions without a GROUP BY clause, it returns one result row for each department, even when no employees match.
For a department without employees, COUNT(*) returns 0, while AVG(Salary) and SUM(Salary) return NULL.
This technique is useful for calculating department-level statistics in a single query.
SQL Server CROSS APPLY vs OUTER APPLY
SQL Server provides two APPLY operators: CROSS APPLY and OUTER APPLY. Both evaluate the right-side table expression for each row of the left-side table, but they handle empty results differently.
| Feature | CROSS APPLY | OUTER APPLY |
|---|---|---|
| Matching right-side rows | Included | Included |
| Left rows without right-side results | Excluded | Preserved |
| Missing right-side values | Not returned | Returned as NULL |
| Similar join behavior | INNER JOIN | LEFT JOIN |
| Main purpose | Return rows with related results | Preserve all left-side rows |
CROSS APPLY Example:
SELECT
d.DepartmentName,
e.EmployeeName
FROM Departments AS d
CROSS APPLY
(
SELECT EmployeeName
FROM Employees
WHERE DepartmentID = d.DepartmentID
) AS e;
OUTER APPLY Example:
SELECT
d.DepartmentName,
e.EmployeeName
FROM Departments AS d
OUTER APPLY
(
SELECT EmployeeName
FROM Employees
WHERE DepartmentID = d.DepartmentID
) AS e;
The CROSS APPLY query excludes departments without employees. The OUTER APPLY query preserves those departments and returns NULL for EmployeeName when no matching employee exists.
SQL Server CROSS APPLY vs INNER JOIN
CROSS APPLY and INNER JOIN can produce similar results when the right-side expression is a simple correlated query.
However, CROSS APPLY is especially useful when the right-side table expression needs to reference columns from the left-side table or return a variable number of rows for each input row.
| Feature | CROSS APPLY | INNER JOIN |
|---|---|---|
| Correlated table expression | Supported | Not directly in the same way |
| Table-valued functions | Commonly used | Can join function results too |
| TOP per left-side row | Convenient | Usually requires another query pattern |
| Matching rows | Included | Included |
| Unmatched left rows | Excluded | Excluded |
For example, CROSS APPLY makes it straightforward to retrieve the top two orders for each customer without using a separate ROW_NUMBER() query.
Practical SQL Server CROSS APPLY Query
The following complete example creates two tables, inserts sample records, and demonstrates how CROSS APPLY retrieves the highest-paid employee from each department.
CREATE TABLE Departments
(
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);
CREATE TABLE Employees
(
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(50),
DepartmentID INT,
Salary DECIMAL(10, 2)
);
INSERT INTO Departments
(DepartmentID, DepartmentName)
VALUES
(101, 'IT'),
(102, 'HR'),
(103, 'Finance');
INSERT INTO Employees
(EmployeeID, EmployeeName, DepartmentID, Salary)
VALUES
(1, 'John', 101, 50000),
(2, 'David', 101, 60000),
(3, 'Sarah', 102, 45000),
(4, 'Michael', 102, 55000);
SELECT
d.DepartmentID,
d.DepartmentName,
e.EmployeeName,
e.Salary
FROM Departments AS d
CROSS APPLY
(
SELECT TOP (1)
EmployeeName,
Salary
FROM Employees
WHERE DepartmentID = d.DepartmentID
ORDER BY Salary DESC, EmployeeID ASC
) AS e;
Expected result:
| DepartmentID | DepartmentName | EmployeeName | Salary |
|---|---|---|---|
| 101 | IT | David | 60000 |
| 102 | HR | Michael | 55000 |
The Finance department does not appear because it has no matching employees.
This example demonstrates how SQL Server CROSS APPLY can retrieve the highest-paid employee for each department using a correlated subquery.