mukti.roy (1/12/2012)
I think paul last Query and my Query both are same.
They will produce exactly the same results, yes. My comments would be that you should avoid the NOLOCK hint, adopt a clear layout style to help people understand your code, qualify names correctly, use aliases, and pay attention to casing ('username' versus 'UserName'):
SELECT
U.*
FROM dbo.Users AS U
JOIN
(
SELECT
U2.UserName,
MAX(U2.GWP) AS MGWP
FROM dbo.Users AS U2
GROUP BY
U2.UserName
) AS A ON
A.UserName = U.UserName
AND A.MGWP = U.GWP
If you're interested in why both query forms produce the execution plans they do, I wrote about that here:
http://sqlblog.com/blogs/paul_white/archive/2010/07/28/the-segment-top-query-optimisation.aspx
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi