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


Different between two query


Different between two query

Author
Message
omid.shokri
omid.shokri
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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.
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2181 Visits: 12539
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.
omid.shokri
omid.shokri
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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.
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2181 Visits: 12539
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...
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16461 Visits: 13207
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 SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
omid.shokri
omid.shokri
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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)


vwismoovb77315
vwismoovb77315
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 117
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.
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