Cross Apply Example: SQL Server Cheat Sheet

Andrew Jul 18, 2026

Sure, here is a comprehensive, SEO-optimized, and human-like article about "cross apply example" following all the rules and guidelines provided.

The T-SQL CROSS APPLY operator is a vital tool for database professionals, enabling transitive joins between tables. This operator returns a row from the outer table for each combination of rows in the outer table that matches any row in the inner table. Let's dive into detailed examples to understand how to use and benefit from CROSS APPLY.

Cross text symbol
Cross text symbol

Before we proceed with examples, let's first understand the basic syntax of CROSS APPLY. The syntax is as follows:

Cross ⸸✞☦✝✟♱☥♁✙⚰️⛧ (emoji text symbol copy paste)
Cross ⸸✞☦✝✟♱☥♁✙⚰️⛧ (emoji text symbol copy paste)

```sql SELECT column1, column2, ... FROM table_name CROSS APPLY (SELECT column1, column2, ... FROM table_name WHERE condition); ```

CROSS APPLY with Single Table

Let's start with a simple example where we apply CROSS APPLY with a single table. Suppose we have a Customers table as shown below:

the cross symbols are shown in black and white, with different names on each one
the cross symbols are shown in black and white, with different names on each one

```sql CREATE TABLE Customers ( CustomerID INT, CustomerName VARCHAR(50), Country VARCHAR(50) ); ```

We want to fetch all customers along with their order details, but the Orders table does not exist. We can still use CROSS APPLY to achieve this:

```sql SELECT C.CustomerID, C.CustomerName, C.Country, O.OrderID, O.OrderDate FROM Customers C CROSS APPLY (SELECT TOP 1 OrderID, OrderDate FROM Orders WHERE CustomerID = C.CustomerID ORDER BY OrderDate DESC) O; ```

Cross Apply with Multiple Tables

Cross Text Symbol Just Copy and Paste it in Text
Cross Text Symbol Just Copy and Paste it in Text

Now, let's consider a more complex scenario where we need to join three or more tables using CROSS APPLY. Suppose we have Orders, OrderDetails, and Products tables. We want to fetch order details along with product information:

```sql CREATE TABLE Orders ( OrderID INT, CustomerID INT, OrderDate DATE ); CREATE TABLE OrderDetails ( OrderID INT, ProductID INT, Quantity INT ); CREATE TABLE Products ( ProductID INT, ProductName VARCHAR(50), Price DECIMAL(5,2) ); ```

We can achieve this using CROSS APPLY as follows:

```sql SELECT O.OrderID, O.OrderDate, OD.Quantity, P.ProductName, P.Price FROM Orders O CROSS APPLY OrderDetails OD CROSS APPLY Products P WHERE O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID; ```

Cross Apply with Subquery

an old, black and white paper with writing on the bottom corner in cursive handwriting
an old, black and white paper with writing on the bottom corner in cursive handwriting

CROSS APPLY can also be used with subqueries. In this example, we'll fetch the top 2 products sold per customer:

```sql SELECT C.CustomerID, C.CustomerName, P.ProductName, SUM(OD.Quantity) as TotalQuantity FROM Customers C CROSS APPLY (SELECT TOP 2 OD.ProductID, SUM(OD.Quantity) as TotalQuantity FROM OrderDetails OD WHERE OD.CustomerID = C.CustomerID GROUP BY OD.ProductID ORDER BY TotalQuantity DESC) P GROUP BY C.CustomerID, C.CustomerName, P.ProductName; ```

CROSS APPLY vs OUTER APPLY

While CROSS APPLY returns only the matched rows from both tables, OUTER APPLY returns all rows from the outer table and matched rows from the inner table. Let's consider an example to understand the difference:

how to make the sign of the cross for kids and adults with instructions on how to do it
how to make the sign of the cross for kids and adults with instructions on how to do it

The following example fetches CustomerID and all associated Products (ProductName) using CROSS APPLY:

```sql CREATE TABLE Customers ( CustomerID INT, CustomerName VARCHAR(50) ); CREATE TABLE Products ( ProductID INT, CustomerID INT, ProductName VARCHAR(50) ); SELECT C.CustomerID, C.CustomerName, P.ProductName FROM Customers C CROSS APPLY dbo.Products P WHERE C.CustomerID = P.CustomerID; ```

However, if we use OUTER APPLY instead, we'll get all customers and their corresponding products if available. If a customer doesn't have any products, row(s) for that customer will still appear in the results, with null values for ProductName:

a cross that has been cut out to be used as a coloring page for children
a cross that has been cut out to be used as a coloring page for children
Google Image Result for https://images.squarespace-cdn.com/content/v1/5a970c95b40b9df3acd98574/1613079082555-YH0CO0XEOA7520OFPPEZ/how-to-make-the-sign-of-the-cross-cnt-mls.jpg
Google Image Result for https://images.squarespace-cdn.com/content/v1/5a970c95b40b9df3acd98574/1613079082555-YH0CO0XEOA7520OFPPEZ/how-to-make-the-sign-of-the-cross-cnt-mls.jpg
Bee 🐝 McTavish a female.
Bee 🐝 McTavish a female.
the cross symbol collection is shown in black and white, as well as several different types of crosses
the cross symbol collection is shown in black and white, as well as several different types of crosses
a black and white image of a cross
a black and white image of a cross
a cross with arrows pointing to it and the words focus on jesus's response
a cross with arrows pointing to it and the words focus on jesus's response
Download Cross for free
Download Cross for free
Sign of the Cross - 1 Page Foldable Pamphlet
Sign of the Cross - 1 Page Foldable Pamphlet
Sign of the Cross
Sign of the Cross

```sql SELECT C.CustomerID, C.CustomerName, P.ProductName FROM Customers C OUTER APPLY dbo.Products P ON C.CustomerID = P.CustomerID; ```

CROSS APPLY and OUTER APPLY each have their use cases, and choosing the correct one depends on the specific requirements of your query.

In summary, CROSS APPLY is a powerful tool for handling transitive joins in T-SQL. Understanding its syntax and usage can greatly enhance your ability to write efficient and complex queries. To further expand your skills, consider exploring other join operators and combinations of operators that complement CROSS APPLY, such as OUTER JOIN and EXISTS. Happy coding!