<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Retrieving Records with Max Values using TOP</title>
	<atom:link href="http://www.rabidgadfly.com/index.php/2007/08/15/retrieving-records-with-max-values-using-top/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rabidgadfly.com/index.php/2007/08/15/retrieving-records-with-max-values-using-top/</link>
	<description>Glenn Gervais&#039;s methods, tips, and musings on Flash, ColdFusion, XML, and other web technology.</description>
	<lastBuildDate>Wed, 01 Sep 2010 12:20:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: rabidGadfly</title>
		<link>http://www.rabidgadfly.com/index.php/2007/08/15/retrieving-records-with-max-values-using-top/comment-page-1/#comment-42766</link>
		<dc:creator>rabidGadfly</dc:creator>
		<pubDate>Wed, 15 Aug 2007 17:44:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.rabidgadfly.com/?p=45#comment-42766</guid>
		<description>Brian, 

Thanks for the improved solution and the concise explanation. Makes perfect sense.</description>
		<content:encoded><![CDATA[<p>Brian, </p>
<p>Thanks for the improved solution and the concise explanation. Makes perfect sense.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Kotek</title>
		<link>http://www.rabidgadfly.com/index.php/2007/08/15/retrieving-records-with-max-values-using-top/comment-page-1/#comment-42760</link>
		<dc:creator>Brian Kotek</dc:creator>
		<pubDate>Wed, 15 Aug 2007 16:48:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.rabidgadfly.com/?p=45#comment-42760</guid>
		<description>Understand though that using correlated subqueries like that can be quite inefficient, depending on the database platform, schema, and the amount of data. Correlated subqueries are evaluated once for every matching record. Which means that the subquery can potentially run for every cartesian product created in the join between GameScores and Games. Different databases have different approaches to trying to optimize this, but it is definitely something to be aware of.

Consider this alternative:

select gs.scoreID, v.maxScore, g.gameName
from
	GameScores gs
	inner join
	(
	select 
		max(score) maxScore, 
		gameID
	from 
		GameScores
	group by 
		gameID
	) v
		on v.maxScore = gs.score and v.gameID = gs.gameID
	inner join
	Games g
		on gs.gameID = g.gameID

This should give the same results, but it uses an inline view that has no correlation to the rest of the query, which means that it will be more efficient than a correlated subquery.

Note that the original query you posted also suffers from another flaw: what happens if there are two records that tie for the high score for a game? Using Top 1 will essentially drop one of the two records. Probably not a big deal for high scores for a game, but if you were calculating which sales rep had the highest sales for the quarter this might be a much bigger issue. ;-)</description>
		<content:encoded><![CDATA[<p>Understand though that using correlated subqueries like that can be quite inefficient, depending on the database platform, schema, and the amount of data. Correlated subqueries are evaluated once for every matching record. Which means that the subquery can potentially run for every cartesian product created in the join between GameScores and Games. Different databases have different approaches to trying to optimize this, but it is definitely something to be aware of.</p>
<p>Consider this alternative:</p>
<p>select gs.scoreID, v.maxScore, g.gameName<br />
from<br />
	GameScores gs<br />
	inner join<br />
	(<br />
	select<br />
		max(score) maxScore,<br />
		gameID<br />
	from<br />
		GameScores<br />
	group by<br />
		gameID<br />
	) v<br />
		on v.maxScore = gs.score and v.gameID = gs.gameID<br />
	inner join<br />
	Games g<br />
		on gs.gameID = g.gameID</p>
<p>This should give the same results, but it uses an inline view that has no correlation to the rest of the query, which means that it will be more efficient than a correlated subquery.</p>
<p>Note that the original query you posted also suffers from another flaw: what happens if there are two records that tie for the high score for a game? Using Top 1 will essentially drop one of the two records. Probably not a big deal for high scores for a game, but if you were calculating which sales rep had the highest sales for the quarter this might be a much bigger issue. <img src='http://www.rabidgadfly.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
