﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / Difference Between cross apply and cross join / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 02:13:43 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Difference Between cross apply and cross join</title><link>http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx</link><description>Y'all are welcome.  I'm glad it made sense.</description><pubDate>Thu, 24 Mar 2011 12:08:42 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: Difference Between cross apply and cross join</title><link>http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx</link><description>@Jack Nice response, Very helpful.</description><pubDate>Thu, 24 Mar 2011 10:26:11 GMT</pubDate><dc:creator>FairFunk</dc:creator></item><item><title>RE: Difference Between cross apply and cross join</title><link>http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx</link><description>Thank you Jack Corbett.nice explanation and exampleBalamurugan</description><pubDate>Mon, 11 Jan 2010 00:10:23 GMT</pubDate><dc:creator>Balamurugan Shanmugasundaram</dc:creator></item><item><title>RE: Difference Between cross apply and cross join</title><link>http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx</link><description>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:[quote]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.  [/quote]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):[code="sql"]-- create a table to run the function againstCREATE TABLE test2 (id INT, col1 CHAR(1))GO-- create the TVFCREATE 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    ENDGO-- table variable for outer/left inputDECLARE @test1 TABLE (id INT, col1 CHAR(1))-- insert test dataINSERT 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 TVFSELECT    *FROM    @test1 AS one CROSS APPLY    dbo.test(one.id) ; DROP TABLE test2 ; GODROP FUNCTION dbo.test ; [/code]  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.</description><pubDate>Fri, 08 Jan 2010 06:58:18 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: Difference Between cross apply and cross join</title><link>http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx</link><description>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 ....</description><pubDate>Thu, 07 Jan 2010 12:46:42 GMT</pubDate><dc:creator>bitbucket-25253</dc:creator></item><item><title>Difference Between cross apply and cross join</title><link>http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx</link><description>What is the Difference Between cross apply and cross join</description><pubDate>Thu, 07 Jan 2010 11:42:44 GMT</pubDate><dc:creator>Balamurugan Shanmugasundaram</dc:creator></item></channel></rss>