CROSS APPLY and OUTER APPLY basics

suggest change

In SQL Server, APPLY is used when the right-hand side is a table-valued function or a correlated subquery.

Create a Department table to hold information about departments. Then create an Employee table which holds information about the employees. Each employee belongs to a department, so the Employee table has a foreign key to the Department table.

First query selects data from Department table and uses CROSS APPLY to evaluate the Employee table for each record of the Department table. Second query simply joins the Department table with the Employee table and all the matching records are produced.

SELECT *
FROM Department D
CROSS APPLY (
    SELECT *
    FROM Employee E
    WHERE E.DepartmentID = D.DepartmentID
) A
GO
SELECT *
FROM Department D
INNER JOIN Employee E
  ON D.DepartmentID = E.DepartmentID

The two queries produce the same result set. The difference shows up when the right-hand side must be a function of the current outer row.

The first query in Script #2 selects data from Department table and uses OUTER APPLY to evaluate the Employee table for each record of the Department table. For Department rows with no match in Employee, the Employee columns are NULL (the prose refers to rows 5 and 6 of a result that is not shown). The second query uses a LEFT OUTER JOIN and also returns every Department row, including those with no matching Employee.

SELECT *
FROM Department D
OUTER APPLY (
    SELECT *
    FROM Employee E
    WHERE E.DepartmentID = D.DepartmentID
) A
GO
SELECT *
FROM Department D
LEFT OUTER JOIN Employee E
  ON D.DepartmentID = E.DepartmentID
GO

Even though the two queries return the same information, the execution plan can differ a bit. The cost is often similar.

Now comes the time to see where the APPLY operator is really required. In Script #3, I am creating a table-valued function which accepts DepartmentID as its parameter and returns all the employees who belong to this department. The next query selects data from Department table and uses CROSS APPLY to join with the function we created. It passes the DepartmentID for each row from the outer table expression (in our case Department table) and evaluates the function for each row similar to a correlated subquery. The next query uses OUTER APPLY instead of CROSS APPLY. Unlike CROSS APPLY, which returns only matched rows, OUTER APPLY also returns outer rows with no match and places NULLs in the missing columns.

CREATE FUNCTION dbo.fn_GetAllEmployeeOfADepartment (@DeptID AS int)
RETURNS TABLE
AS
  RETURN
  (
  SELECT
    *
  FROM Employee E
  WHERE E.DepartmentID = @DeptID
  )
GO
SELECT
  *
FROM Department D
CROSS APPLY dbo.fn_GetAllEmployeeOfADepartment(D.DepartmentID)
GO
SELECT
  *
FROM Department D
OUTER APPLY dbo.fn_GetAllEmployeeOfADepartment(D.DepartmentID)
GO

A normal join cannot take the place of these function calls. If you replace CROSS APPLY or OUTER APPLY with INNER JOIN or LEFT OUTER JOIN, add an ON clause such as 1 = 1, and run the query, SQL Server reports that the multi-part identifier D.DepartmentID could not be bound. The join’s right-hand side cannot see columns from the left as function arguments. APPLY (or a standard LATERAL join) is what passes the outer value in.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents