CROSS APPLY returns only rows from the outer table that produce a result
set from the table-valued function. It other words, result of CROSS
APPLY doesn’t contain any row of left side table expression for which
no result is obtained from right side table expression. CROSS APPLY
work as a row by row INNER JOIN.
CROSS APPLY Query:
SELECT * FROM ProjectAS PRO
CROSSAPPLY
(SELECT * FROM Employee AS EMP WHERE PRO.Project_Id=EMP.Project_Id)Tab
Hi,
Use of cross apply :
The CROSS APPLY operator returns only those rows from the left table expression (in its final output) if it matches with the right table expression. In other words, the right table expression returns rows for the left table expression match only.
So you might conclude, the CROSS APPLY is equivalent to an INNER JOIN (or to be more precise its like a CROSS JOIN with a correlated sub-query) with an implicit join condition of 1=1 whereas the OUTER APPLY is equivalent to a LEFT OUTER JOIN.
For more detail
https://www.mssqltips.com/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/
CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. It other words, result of CROSS APPLY doesn't contain any row of left sidetable expression for which no result is obtained from right side table expression. CROSS APPLY work as a row by row INNER JOIN.
Example is:-
USE [tempdb]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[Employee]') AND type IN (N'U'))
BEGIN
DROPTABLE [Employee]
END
GO
IF EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[Department]') AND type IN (N'U'))
Shraddha PatelPosted May 21, 2019, 10:45 PM
Amit KumarPosted Apr 30, 2019, 12:07 AM
Shraddha PatelPosted Apr 29, 2019, 11:13 PM
Rajkiran SwainPosted Jun 20, 2017, 5:30 AM
See the article in my blog for detailed performance comparison:
INNER JOINvs.CROSS APPLYCROSS APPLYworks better on things that have no simpleJOINcondition.This one selects
3last records fromt2for each record fromt1:It cannot be easily formulated with an
INNER JOINcondition.You could probably do something like that using
CTE's and window function:, but this is less readable and probably less efficient.
Update:
Just checked.
masteris a table of about20,000,000records with aPRIMARY KEYonid.This query:
runs for almost
30seconds, while this one:is instant.
Sundaram SubramanianPosted Jun 20, 2017, 5:20 AM