NATURAL JOIN Syntax IN SQL Server 2005

  • IN ARTICLE "Joins in SQL Server 2005" (http://www.c-sharpcorner.com/UploadFile/raj1979/SqlJoins10012008164642PM/SqlJoins.aspx) THE AUTHOR TALKS ABOUT USING THE NATURAL JOIN SYNTAX IN T-SQL FOR SQL SERVER 2005.

    I AM UNABLE TO USE THE "NATURAL JOIN" FEATURE IN MY T-SQL CODE.

    THIS FEATURE DOES NOT SEEM TO BE DOCUMENTED IN THE BOOKS ONLINE.

    EXAMPLES:

    SELECT *

    From sys.tables t1

    INNER JOIN sys.tables t2

    ON t1.object_id=t2.object_id;

    GO

    SELECT *

    From sys.tables t1

    NATURAL JOIN sys.tables t2;

    GO

    NOTE THAT THE FIRST ONE WORKS, BUT THE SECOND ONE GIVES A SYNTAX ERROR.

  • There is no NATURAL join available in SQL Server.

  • The author of that article didn't do his homework on joins. There is no Natural Join in SQL Server. And, if you research it a bit, if such a thing did exist in SQL Server, it would be a very bad thing to use as most articles on natural joins in other RDBMS's suggest.

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I think what you are looking for is an INTERSECT query. This "join" returns only the rows matching in all columns:

    [font="Courier New"]DECLARE @t1 TABLE (Id INT, SomeInt INT)

    DECLARE @t2 TABLE (Id INT, SomeInt INT)

    INSERT INTO @t1

                 SELECT 1, 1

       UNION ALL SELECT 2, 1

       UNION ALL SELECT 3, 3

    INSERT INTO @t2

                 SELECT 1, 1

       UNION ALL SELECT 2, 2

       UNION ALL SELECT 3, 3

       UNION ALL SELECT 4, 1

    ; WITH match_all (Id, SomeInt) AS

    (

       SELECT * FROM @t1

       INTERSECT

       SELECT * FROM @t2

    )

    SELECT

          *

       FROM match_all

    [/font]

  • I used Inner, Left , right ,

    cross ,full outer joins .. But Now i am seeing this new Natural Join..

    Amazing ,

    Is it Available in 2008?

  • Hi

    Nope. There is no natural join in SQL Server, even not in SSE2k8. The closed version of a natural join in an INTERSECT.

    Greets

    Flo

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply