Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Difference Between cross apply and cross join Expand / Collapse
Author
Message
Posted Thursday, January 7, 2010 11:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 7, 2012 4:25 AM
Points: 6, Visits: 32
What is the Difference Between cross apply and cross join
Post #843768
Posted Thursday, January 7, 2010 12:46 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 5,586, Visits: 24,935
You have posted your question in a SQL 2000 forum ... CROSS APPLY WAS introduced in SQL Server 2005 ...

For SQL 2000 CROSS JOINS exist and they exist in SQL 2005 as well ... so the difference is .. one exists one does not.

Now if you question pertains to SQL 2005 or 2008 use Books On Line for an explanation and sample code ....


If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #843824
Posted Friday, January 8, 2010 6:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:20 PM
Points: 11,219, Visits: 12,976
CROSS JOIN returns a Cartesian product so if you have 10 rows in each table the query will return 100 rows, 1 row for each combination.

CROSS APPLY from BOL:

The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.


CROSS APPLY is similar to, but in most cased not the same as an INNER JOIN. When calling a TVF the TVF is called/executed for each row in the outer table/left input.

You pass one of the columns from the outer table to the function and the TVF returns the matching rows. You can also use APPLY to supply a value to "join" on a derived table. Here's a simple example of both CROSS JOIN and CROSS APPLY (the cross apply with the derived table in this example would be better written as an INNER JOIN and the Optimizer converts it to one):

-- create a table to run the function against
CREATE TABLE test2 (id INT, col1 CHAR(1))

GO

-- create the TVF
CREATE FUNCTION test (@id INT)
RETURNS @table TABLE (id INT, col1 CHAR(1))
AS BEGIN

INSERT INTO
@table
SELECT
*
FROM
test2 AS T
WHERE
id = @id ;

RETURN
END

GO

-- table variable for outer/left input
DECLARE @test1 TABLE (id INT, col1 CHAR(1))

-- insert test data
INSERT INTO
@test1 (id, col1)
SELECT TOP 10
ROW_NUMBER() OVER (ORDER BY OBJECT_ID),
CHAR(ROW_NUMBER() OVER (ORDER BY OBJECT_ID) + 60)
FROM
sys.all_columns AS AC

INSERT INTO
test2 (id, col1)
SELECT TOP 10
ROW_NUMBER() OVER (ORDER BY OBJECT_ID),
CHAR(ROW_NUMBER() OVER (ORDER BY OBJECT_ID) + 60)
FROM
sys.all_columns AS AC

-- cross join Cartesian product
SELECT
*
FROM
@test1 CROSS JOIN
test2

-- outer apply on a derived table
SELECT
*
FROM
@test1 AS one CROSS APPLY
(
SELECT
*
FROM
test2 AS two
WHERE
one.id = two.id
) AS test2

-- outer apply TVF
SELECT
*
FROM
@test1 AS one CROSS APPLY
dbo.test(one.id) ;

DROP TABLE test2 ;
GO
DROP FUNCTION dbo.test ;




If you SET STATISTICS IO ON you will see that the last query (CROSS APPLY to the TVF) has 10 scans because for each row in @test1 the TVF is being called and going out and scanning the test2 table.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #844265
Posted Monday, January 11, 2010 12:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 7, 2012 4:25 AM
Points: 6, Visits: 32
Thank you Jack Corbett.
nice explanation and example




Balamurugan
Post #845202
Posted Thursday, March 24, 2011 10:26 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 7:51 AM
Points: 47, Visits: 1,358
@Jack Nice response, Very helpful.
Post #1083455
Posted Thursday, March 24, 2011 12:08 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:20 PM
Points: 11,219, Visits: 12,976
Y'all are welcome. I'm glad it made sense.





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1083511
Posted Thursday, June 19, 2014 12:57 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 12:22 PM
Points: 60, Visits: 214
bitbucket-25253....wasted my time what a response
Post #1584039
Posted Friday, June 20, 2014 1:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 7,178, Visits: 13,630
daniarchitect (6/19/2014)
bitbucket-25253....wasted my time what a response


If Bitbucket's explanation doesn't exactly meet your requirements, then simply ask what you need to know. Rudeness is unnecessary and unprofessional.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1584224
Posted Friday, June 20, 2014 9:27 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:34 PM
Points: 36,952, Visits: 31,461
daniarchitect (6/19/2014)
bitbucket-25253....wasted my time what a response


BWAA-HAA!!! Considering that you even went back and edited your response, I suspect you voluntarily wasted more time on your own that what you claim BitBucket did.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1584411
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse