Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Difference Between cross apply and cross join


Difference Between cross apply and cross join

Author
Message
Balamurugan Shanmugasundaram
Balamurugan Shanmugasundaram
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 32
What is the Difference Between cross apply and cross join
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5683 Visits: 25280
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
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11014 Visits: 14858
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
Balamurugan Shanmugasundaram
Balamurugan Shanmugasundaram
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 32
Thank you Jack Corbett.
nice explanation and example




Balamurugan
FairFunk
FairFunk
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 1650
@Jack Nice response, Very helpful.
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11014 Visits: 14858
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
daniarchitect
daniarchitect
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 327
bitbucket-25253....wasted my time what a response
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8923 Visits: 19009
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44806 Visits: 39847
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
rashiki777
rashiki777
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 3
bitbucket-25253 (1/7/2010)
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 ....


That's a pretty jerk answer.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search