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

Different between two query Expand / Collapse
Author
Message
Posted Sunday, July 7, 2013 3:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 12:40 AM
Points: 11, Visits: 107
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.
Post #1470969
Posted Sunday, July 7, 2013 4:14 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 12:38 PM
Points: 839, Visits: 5,389
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.
Post #1470998
Posted Sunday, July 7, 2013 9:24 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 12:40 AM
Points: 11, Visits: 107
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.
Post #1471023
Posted Sunday, July 7, 2013 10:09 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 12:38 PM
Points: 839, Visits: 5,389
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...
Post #1471029
Posted Monday, July 8, 2013 12:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:24 PM
Points: 13,239, Visits: 11,017
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.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1471051
Posted Monday, July 8, 2013 1:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 12:40 AM
Points: 11, Visits: 107
The table definition is:

CREATE TABLE CustomerSegment
(
Code INT PRIMARY KEY,
rowguid UNIQUEIDENTIFIER DEFAULT(NEWID())
)

CREATE TABLE Person
(
Code INT PRIMARY KEY,
CustomerSegment INT REFERENCES [dbo].[CustomerSegment]([Code]),
rowguid UNIQUEIDENTIFIER DEFAULT(NEWID())
)

CREATE TABLE CustomerCreditD
(
CustomerSegment INT REFERENCES [dbo].[CustomerSegment]([Code]),
rowguid UNIQUEIDENTIFIER PRIMARY KEY DEFAULT(NEWID())
)

CREATE TABLE CustomerCreditException
(
Person INT REFERENCES [dbo].[Person]([Code]),
rowguid UNIQUEIDENTIFIER 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:
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)

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)

Post #1471073
Posted Monday, July 8, 2013 7:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 11:02 AM
Points: 15, Visits: 95

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.
Post #1471190
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse