SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find the correct answer in the following query


Find the correct answer in the following query

Author
Message
aaron.reese
aaron.reese
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1071 Visits: 902
Thanks for the explanation guys, I learned something today.
Actually 2 things. 1) scope and 2) yet another reason not to use subqueries



@kingston, It is a result but I would argue not the correct result. I can't imagine a scenario where you would write the query like that for the result described.

Personally I avoid subqueries whereever possible; I find they produce untidy code that is difficult to maintain and whereever possible I will use Common Table Expressions and then use an appropriate join in the main query. This has Three main benefits:

1) it keeps code clean
2) the same CTE is reusable a number of times
3) it preserves scope.

E.g Return all the details of the last order a customer placed.

SELECT
C.CustomerID,
O,*
FROM
Customer C
JOIN
(/* Last Order For Each Customer*/
SELECT CustomerID,max(orderID) from Orders O Group by O.CustomerID
) LO on LO.CustomerID = C.CustomerID
JOIN
Orders O on O.OrderID = LO.OrderID



Compared to

WITH CTE_LastOrder as
(/* Last Order For Each Customer*/
SELECT CustomerID,max(orderID) from Orders O Group by O.CustomerID
)

SELECT
C.CustomerID,
O,*
FROM
Customer C
JOIN
CTE_LastOrder LO on LO.CustomerID = C.CustomerID
JOIN
Orders O on O.OrderID = LO.OrderID



I find the second easier to read and maintain. When looking at the main code I don't have to rethink what the subquery is doing each time, I can get the gist from the suitably named CTE. I can fiddle with the CTE and test the code independently of the main query. When building very complex queries, you can build a series of CTEs that build upon one another and re-use them.

anayway I have gone WAAAAAY off topic. We should really answer the OPs question which is 100: All the customers in the customer list.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90927 Visits: 45284
aaron.reese (6/20/2013)
Thanks for the explanation guys, I learned something today.
Actually 2 things. 1) scope and 2) yet another reason not to use subqueries


Correlated subqueries can be incredibly powerful, they're not something to avoid, just to understand.

It is a result but I would argue not the correct result. I can't imagine a scenario where you would write the query like that for the result described.


The query is likely written incorrectly, however the results are correct for the way it's written.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


iiit.raju
iiit.raju
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 161
Ya i created and saw the result.

Thanks all
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