5
Answers

Are Inner join and Cross Apply same in SQL Server?

Dinesh Sub

Dinesh Sub

Jun 14
522
1

 Are inner join and cross apply equivalent. Also is Left Outer Join and Outer apply same, then why create this new way?

Answers (5)
4
Jignesh Kumar

Jignesh Kumar

29 39.5k 2.9m Jun 18

Hello Dinesh,

Both are completely different and have different usage purposes,

Please find below the details,
 

When to Use Which?

  • Use INNER JOIN when you need to combine rows from two tables based on a specific condition.
  • Use CROSS APPLY when you need to perform more complex operations that involve invoking a table-valued function or subquery for each row of the outer table, especially when the right side's data depends on the left side's data
2
Jaimin Shethiya

Jaimin Shethiya

48 30.6k 599.7k Jun 16

Hello,

Please refer below difference.

Inner join selects the rows that satisfies both the table.

Cross join selects the all the rows from the first table and all the rows from second table and shows as Cartesian product ie, with all possibilities.

 

Thanks

2
Naveen Kumar

Naveen Kumar

161 11.8k 284.8k Jun 15

No, INNER JOIN and CROSS APPLY are not equivalent. They serve different purposes and are used in different contexts in SQL Server.

INNER JOIN vs CROSS APPLY

INNER JOIN:

  • Combines rows from two tables based on a related column between them.
  • Requires a specified condition for matching rows.
  • Returns only the rows where there is a match in both tables.
SELECT a.*, b.*
FROM TableA a
INNER JOIN TableB b ON a.Id = b.Id;

CROSS APPLY:

  • Used to join a table with a table-valued function, subquery, or another table.
  • Evaluates the right-hand-side expression (like a subquery or function) for each row of the left-hand-side table.
  • Returns only rows from the left table where the apply operation produces a result.
SELECT a.*, b.*
FROM TableA a
CROSS APPLY (SELECT * FROM TableB b WHERE a.Id = b.Id) b;
2
Ishika Tiwari

Ishika Tiwari

224 8.6k 467k Jun 15
  1. CROSS APPLY and INNER JOIN are not similar. In the left table, CROSS APPLY analyzes an expression for every row, whereas INNER JOIN matches rows according to a criteria.
  2. While they are not the same, LEFT OUTER JOIN and OUTER APPLY are similar. While OUTER APPLY checks an expression for each row and includes NULLs if the expression returns no rows, LEFT OUTER JOIN matches rows and fills in NULLs when there are no matches.
  3. When dealing with complex and correlated subquery conditions that are difficult for standard joins to manage, the APPLY operator offers greater flexibility.
     
2
Uday Dodiya

Uday Dodiya

75 25k 1.5m Jun 15

Inner Join and Cross Apply, as well as Left Outer Join and Outer Apply, serve different purposes and are not entirely equivalent. Here is a detailed comparison:

Inner Join vs. Cross Apply

  • Inner Join:

    • Combines rows from two tables based on a specified condition.
    • Only rows that satisfy the condition are included in the result set.
  • Cross Apply:

    • Used to apply a table-valued function to each row of an outer table.
    • Can be used to join a table to a derived table or a table-valued function.
    • The result is similar to an Inner Join when the table-valued function returns rows.

Example:

-- Inner Join
SELECT a.Column1, b.Column2
FROM TableA a
INNER JOIN TableB b ON a.Id = b.AId

-- Cross Apply
SELECT a.Column1, b.Column2
FROM TableA a
CROSS APPLY (SELECT Column2 FROM TableB WHERE TableB.AId = a.Id) b

Key Differences:

  • Inner Join works with two base tables.
  • Cross Apply works with a table and a table-valued function, allowing for more complex queries and dynamic result sets.

Left Outer Join vs. Outer Apply

  • Left Outer Join:

    • Combines rows from two tables and includes all rows from the left table, even if there are no matching rows in the right table.
    • Non-matching rows from the right table result in NULL values in the result set.
  • Outer Apply:

    • Similar to Left Outer Join but used with table-valued functions.
    • Includes all rows from the left table and uses the table-valued function to determine the matching rows from the right table.

Example:
 

-- Left Outer Join
SELECT a.Column1, b.Column2
FROM TableA a
LEFT OUTER JOIN TableB b ON a.Id = b.AId

-- Outer Apply
SELECT a.Column1, b.Column2
FROM TableA a
OUTER APPLY (SELECT Column2 FROM TableB WHERE TableB.AId = a.Id) b

Key Differences:

  • Left Outer Join works with two base tables.
  • Outer Apply works with a table and a table-valued function, allowing for complex and dynamic queries.

Why Use Apply?

  1. Flexibility: CROSS APPLY and OUTER APPLY provide the ability to join with derived tables and table-valued functions, offering more flexibility than traditional joins.

  2. Dynamic Queries: They allow for dynamic calculations and subqueries that depend on the outer query's row values.

  3. Complex Scenarios: Useful in scenarios where the relationship between tables is not straightforward or static, enabling more complex data retrieval patterns.

Summary

While INNER JOIN and CROSS APPLY can sometimes produce similar results, they are used in different contexts and are not equivalent. The same applies to LEFT OUTER JOIN and OUTER APPLY. The APPLY operators provide additional functionality, especially when working with table-valued functions or complex subqueries, which traditional joins cannot achieve directly.