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.

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

```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:

```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

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

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:

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:









```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!