Different between two query

  • Hi Dear,

    What is different between two query?

    query 1:

    SELECT dbo.Person.Code

    FROM dbo.Person LEFT OUTER JOIN

    dbo.CustomerSegment ON dbo.Person.CustomerSegment = dbo.CustomerSegment.Code INNER JOIN

    dbo.CustomerCreditD ON dbo.CustomerSegment.Code = dbo.CustomerCreditD.CustomerSegment LEFT OUTER JOIN

    dbo.CustomerCreditException ON dbo.CustomerCreditException.Person = dbo.Person.Code

    WHERE (dbo.Person.Code = 22122738)

    query 2:

    SELECT dbo.Person.Code

    FROM dbo.CustomerCreditD INNER JOIN

    dbo.CustomerSegment ON dbo.CustomerCreditD.CustomerSegment = dbo.CustomerSegment.Code RIGHT OUTER JOIN

    dbo.Person LEFT OUTER JOIN

    dbo.CustomerCreditException ON dbo.CustomerCreditException.Person = dbo.Person.Code ON dbo.CustomerSegment.Code = dbo.Person.CustomerSegment

    WHERE (dbo.Person.Code = 22122738)

    Thanks.

  • Is this homework? When you ran it, did you notice any differences?

    OUTER joins show all the records from the LEFT table, and matches or nulls in the right side table.

  • Thanks for reply.

    This query is the part of a procedure.

    I think no different these two query, but output of query 1 is without any row. In other word, "Inner Join dbo.CustomerCreditD" is cause of the result.

  • omid.shokri (7/7/2013)


    Thanks for reply.

    This query is the part of a procedure.

    I think no different these two query, but output of query 1 is without any row. In other word, "Inner Join dbo.CustomerCreditD" is cause of the result.

    There's a significant difference between an INNER join and an OUTER join. With an inner join, table sequence doesn't matter that much, but with an OUTER join, it's a big deal.

    Maybe you should test on really small data sets - maybe 5-10 records in each table, maximum. Then once you can examine the results and make sense of how you got there, you'll understand it better. One way of doing something like that is to insert a small handful of records into a temporary table, and then query the temporary table...

  • Just avoid RIGHT JOIN altogether. It makes it harder to follow the logic from top to bottom. Also use brackets around your tables so you can easily see what's going on.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • The table definition is:

    CREATE TABLE CustomerSegment

    (

    Code INT PRIMARY KEY,

    rowguidUNIQUEIDENTIFIER DEFAULT(NEWID())

    )

    CREATE TABLE Person

    (

    Code INT PRIMARY KEY,

    CustomerSegment INT REFERENCES [dbo].[CustomerSegment]([Code]),

    rowguidUNIQUEIDENTIFIER DEFAULT(NEWID())

    )

    CREATE TABLE CustomerCreditD

    (

    CustomerSegment INT REFERENCES [dbo].[CustomerSegment]([Code]),

    rowguidUNIQUEIDENTIFIER PRIMARY KEY DEFAULT(NEWID())

    )

    CREATE TABLE CustomerCreditException

    (

    Person INT REFERENCES [dbo].[Person]([Code]),

    rowguidUNIQUEIDENTIFIER PRIMARY KEY DEFAULT(NEWID())

    )

    INSERT INTO [dbo].[CustomerSegment]

    ([Code])

    VALUES(100)

    INSERT INTO [dbo].[Person]

    ([Code],[CustomerSegment])

    VALUES(22122738, 100)

    INSERT INTO [dbo].[CustomerCreditException]

    ([Person])

    VALUES(22122738)

    no result for query 1:

    SELECTdbo.Person.Code

    FROMdbo.Person

    LEFT OUTER JOIN dbo.CustomerSegment ON dbo.Person.CustomerSegment = dbo.CustomerSegment.Code

    INNER JOIN dbo.CustomerCreditD ON dbo.CustomerSegment.Code = dbo.CustomerCreditD.CustomerSegment

    LEFT OUTER JOIN dbo.CustomerCreditException ON dbo.CustomerCreditException.Person = dbo.Person.Code

    WHERE(dbo.Person.Code = 22122738)

    but result for query 2:

    SELECT dbo.Person.Code

    FROM dbo.CustomerCreditD INNER JOIN

    dbo.CustomerSegment ON dbo.CustomerCreditD.CustomerSegment = dbo.CustomerSegment.Code RIGHT OUTER JOIN

    dbo.Person LEFT OUTER JOIN

    dbo.CustomerCreditException ON dbo.CustomerCreditException.Person = dbo.Person.Code ON dbo.CustomerSegment.Code = dbo.Person.CustomerSegment

    WHERE (dbo.Person.Code = 22122738)

  • Query 1 will return Person Code from Person table only where the Person has its CustomerSegment value found in the CustomerSegment table and in the CustomerCreditD table. This is because of the INNER JOIN involving CustomerCreditD and CustomerSegment.

    Query 2 will return Person Code from the Person table as long as it is found in it. This is because the RIGHT OUTER JOIN to the Person table ensures that the row from the Person table matching your selection criteria will be included in the results regardless if there is matching rows from the earlier tables and the left joined subsequent table.

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

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