 




<?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: SQL query How to select nth row from a table if they are not in any order</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/</link>
	<description></description>
	<lastBuildDate>Sun, 19 May 2013 03:14:28 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: tomliotta</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79869</link>
		<dc:creator>tomliotta</dc:creator>
		<pubDate>Fri, 30 Jul 2010 21:24:37 +0000</pubDate>
		<guid isPermaLink="false">#comment-79869</guid>
		<description><![CDATA[&lt;i&gt;...the ‘nth’ position record is only meaningful for the current result set of the query...&lt;/i&gt;

Sorry, but this isn&#039;t true for the RRN() function. RRN(1) is not equivalent to the first position in the result set and RRN(n) is not equivalent to the nth position in the result set.

The RRN() function refers to physical order of the underlying table as it appears on disk. This is very different from the &#039;result set&#039; which doesn&#039;t exist at all until the statement executes. A RRN() value will most likely be different from a ROW_NUMBER value for the same row -- ROW_NUMBER is a reference to position in the result set. The two values may be in exactly opposite order if ORDER BY is descending over RRN().

A physical &#039;record&#039; is not the same as a &#039;result set row&#039;. Physical records are read from disk in order to generate result set rows. Result sets are logical rather than physical.

The existence of a function such as RRN() is dependent on the particular DBMS. I don&#039;t think that MS SQL Server has a similar function, but SQL for DB2 on System i does. Oracle... I don&#039;t know.

In any case, it depends on what the OP means by &quot;order&quot; and &quot;nth row&quot; as well as the platform that&#039;s available to the OP.

Tom]]></description>
		<content:encoded><![CDATA[<p><i>&#8230;the ‘nth’ position record is only meaningful for the current result set of the query&#8230;</i></p>
<p>Sorry, but this isn&#8217;t true for the RRN() function. RRN(1) is not equivalent to the first position in the result set and RRN(n) is not equivalent to the nth position in the result set.</p>
<p>The RRN() function refers to physical order of the underlying table as it appears on disk. This is very different from the &#8216;result set&#8217; which doesn&#8217;t exist at all until the statement executes. A RRN() value will most likely be different from a ROW_NUMBER value for the same row &#8212; ROW_NUMBER is a reference to position in the result set. The two values may be in exactly opposite order if ORDER BY is descending over RRN().</p>
<p>A physical &#8216;record&#8217; is not the same as a &#8216;result set row&#8217;. Physical records are read from disk in order to generate result set rows. Result sets are logical rather than physical.</p>
<p>The existence of a function such as RRN() is dependent on the particular DBMS. I don&#8217;t think that MS SQL Server has a similar function, but SQL for DB2 on System i does. Oracle&#8230; I don&#8217;t know.</p>
<p>In any case, it depends on what the OP means by &#8220;order&#8221; and &#8220;nth row&#8221; as well as the platform that&#8217;s available to the OP.</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chippy088</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79862</link>
		<dc:creator>chippy088</dc:creator>
		<pubDate>Fri, 30 Jul 2010 19:52:17 +0000</pubDate>
		<guid isPermaLink="false">#comment-79862</guid>
		<description><![CDATA[Maybe the question means that the command syntax is required to display row n in a sql database. It&#039;s homework. (Doesn&#039;t the short format of the question ring bells?)

I believe SQL is implicit in the question heading.

If selecting/displaying the Nth row is required, then there must be an order, otherwise the Nth position would be meaningless.

Without a more precise definition of the problem, there is no point in trying to guess the answer. We are after all said and done IT professionals and not mind readers.]]></description>
		<content:encoded><![CDATA[<p>Maybe the question means that the command syntax is required to display row n in a sql database. It&#8217;s homework. (Doesn&#8217;t the short format of the question ring bells?)</p>
<p>I believe SQL is implicit in the question heading.</p>
<p>If selecting/displaying the Nth row is required, then there must be an order, otherwise the Nth position would be meaningless.</p>
<p>Without a more precise definition of the problem, there is no point in trying to guess the answer. We are after all said and done IT professionals and not mind readers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kccrosser</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79856</link>
		<dc:creator>kccrosser</dc:creator>
		<pubDate>Fri, 30 Jul 2010 16:44:43 +0000</pubDate>
		<guid isPermaLink="false">#comment-79856</guid>
		<description><![CDATA[Keep in mind also that database engine updates can result in changes to the default result list order when no Order By clause is specified.
Also, when the distribution of key values in the table changes, this can cause the result set to return in a different order depending on how the query optimizer decides to execute the query.

So - the short answer is &quot;If there is no order specified in a query, the &#039;nth&#039; position record is only meaningful for the current result set of the query - rerunning the same query again may result in a different record occupying the same &#039;nth&#039; position.&quot;

If the OP was trying to implement some record set &quot;browser&quot; that could navigate forwards and backwards through the current unordered static set of records returned from a query, I would just write a set of stored procedures that read the data into a static temporary table and assigned sequential row number values, then provide methods to retrieve the data from that static table by the assigned row numbers.]]></description>
		<content:encoded><![CDATA[<p>Keep in mind also that database engine updates can result in changes to the default result list order when no Order By clause is specified.<br />
Also, when the distribution of key values in the table changes, this can cause the result set to return in a different order depending on how the query optimizer decides to execute the query.</p>
<p>So &#8211; the short answer is &#8220;If there is no order specified in a query, the &#8216;nth&#8217; position record is only meaningful for the current result set of the query &#8211; rerunning the same query again may result in a different record occupying the same &#8216;nth&#8217; position.&#8221;</p>
<p>If the OP was trying to implement some record set &#8220;browser&#8221; that could navigate forwards and backwards through the current unordered static set of records returned from a query, I would just write a set of stored procedures that read the data into a static temporary table and assigned sequential row number values, then provide methods to retrieve the data from that static table by the assigned row numbers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79855</link>
		<dc:creator>carlosdl</dc:creator>
		<pubDate>Fri, 30 Jul 2010 16:23:09 +0000</pubDate>
		<guid isPermaLink="false">#comment-79855</guid>
		<description><![CDATA[&quot;&lt;i&gt;But note that the order of records retrieved isn’t important here&lt;/i&gt;&quot;

That&#039;s true, mainly because the OP was asking about a query that would return just one row, the nth one.

If the RRN() function (or some other equivalent) uses the physical order on disk, but some operations could change that physical order, using it to get the nth record &lt;b&gt;could&lt;/b&gt; be risky (IMO) , but it could be the only option if there is really no other order as the OP said.

Interesting discussion.  Unfortunately, the OP wasn&#039;t involved.]]></description>
		<content:encoded><![CDATA[<p>&#8220;<i>But note that the order of records retrieved isn’t important here</i>&#8221;</p>
<p>That&#8217;s true, mainly because the OP was asking about a query that would return just one row, the nth one.</p>
<p>If the RRN() function (or some other equivalent) uses the physical order on disk, but some operations could change that physical order, using it to get the nth record <b>could</b> be risky (IMO) , but it could be the only option if there is really no other order as the OP said.</p>
<p>Interesting discussion.  Unfortunately, the OP wasn&#8217;t involved.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tomliotta</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79842</link>
		<dc:creator>tomliotta</dc:creator>
		<pubDate>Fri, 30 Jul 2010 06:13:07 +0000</pubDate>
		<guid isPermaLink="false">#comment-79842</guid>
		<description><![CDATA[&lt;i&gt;If no order is specified, there is no way to be completely sure that you will&lt;/i&gt; get the results in the same order &lt;i&gt;every time you run the same query against the same data...&lt;/i&gt;

Yes, very much agreed. But note that the order of records retrieved isn&#039;t important here. The &lt;i&gt;result set&lt;/i&gt; will have no guaranteed order, but the set of rows will be the same. They might be ordered 1, 2, 3 one time and 3, 2, 1 the next time. But the same rows will be there -- unless the file is changed, by reorganization, by altered attributes or by UPDATE, INSERT or DELETE.

Of course, when a file is changed, you should expect a different set of rows to be returned. However, the set will still be those defined by the WHERE clause. In the case of the RRN() function, they will be the records from the specified physical positions regardless of their position in the final result set... assuming that the DBMS has a RRN() or a similar function.

Tom]]></description>
		<content:encoded><![CDATA[<p><i>If no order is specified, there is no way to be completely sure that you will</i> get the results in the same order <i>every time you run the same query against the same data&#8230;</i></p>
<p>Yes, very much agreed. But note that the order of records retrieved isn&#8217;t important here. The <i>result set</i> will have no guaranteed order, but the set of rows will be the same. They might be ordered 1, 2, 3 one time and 3, 2, 1 the next time. But the same rows will be there &#8212; unless the file is changed, by reorganization, by altered attributes or by UPDATE, INSERT or DELETE.</p>
<p>Of course, when a file is changed, you should expect a different set of rows to be returned. However, the set will still be those defined by the WHERE clause. In the case of the RRN() function, they will be the records from the specified physical positions regardless of their position in the final result set&#8230; assuming that the DBMS has a RRN() or a similar function.</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79831</link>
		<dc:creator>carlosdl</dc:creator>
		<pubDate>Fri, 30 Jul 2010 00:59:47 +0000</pubDate>
		<guid isPermaLink="false">#comment-79831</guid>
		<description><![CDATA[Agreed.  That&#039;s what I meant.

When one talks about the nth position, an order is necessarily involved.  It could be the order of how rows are stored on disk, the order of how they are displayed when no order is specified (which is not necessarily the same), or an order specified with an ORDER BY clause in a query.

&quot;&lt;i&gt;If I reorganize the physical and cause the rows to occupy different physical disk positions, I will get the new row that comes first on disk.&lt;/i&gt;&quot;

That&#039;s the problem.  If no order is specified, there is no way to be completely sure that you will get the results in the same order every time you run the same query against the same data (some DBMSs might apply a &#039;default&#039; order criteria when no order is specified (the pk, for example), but I wouldn&#039;t rely on that).]]></description>
		<content:encoded><![CDATA[<p>Agreed.  That&#8217;s what I meant.</p>
<p>When one talks about the nth position, an order is necessarily involved.  It could be the order of how rows are stored on disk, the order of how they are displayed when no order is specified (which is not necessarily the same), or an order specified with an ORDER BY clause in a query.</p>
<p>&#8220;<i>If I reorganize the physical and cause the rows to occupy different physical disk positions, I will get the new row that comes first on disk.</i>&#8221;</p>
<p>That&#8217;s the problem.  If no order is specified, there is no way to be completely sure that you will get the results in the same order every time you run the same query against the same data (some DBMSs might apply a &#8216;default&#8217; order criteria when no order is specified (the pk, for example), but I wouldn&#8217;t rely on that).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tomliotta</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79830</link>
		<dc:creator>tomliotta</dc:creator>
		<pubDate>Fri, 30 Jul 2010 00:35:26 +0000</pubDate>
		<guid isPermaLink="false">#comment-79830</guid>
		<description><![CDATA[&lt;i&gt;And that involves an order as well…&lt;/i&gt;

As long as that means &quot;a physical order of how rows are stored on disk&quot;, then I suppose that that&#039;s true. If it relates to a specification such as ORDER BY as understood within SQL, not so.

An example that I just ran under DB2 to select only physical record #1:&lt;pre&gt;
SELECT rrn(a) FROM accp a WHERE rrn(a)=1&lt;/pre&gt;
No ORDER BY specified on the statement.

If I reorganize the physical and cause the rows to occupy different physical disk positions, I will get the new row that comes first on disk.

I suppose a clear concept of &quot;order&quot; would need to be supplied. The OP probably needs to be more clear. Row number in a result set and physical record number are usually different values.

Tom]]></description>
		<content:encoded><![CDATA[<p><i>And that involves an order as well…</i></p>
<p>As long as that means &#8220;a physical order of how rows are stored on disk&#8221;, then I suppose that that&#8217;s true. If it relates to a specification such as ORDER BY as understood within SQL, not so.</p>
<p>An example that I just ran under DB2 to select only physical record #1:
<pre>
SELECT rrn(a) FROM accp a WHERE rrn(a)=1</pre>
<p>No ORDER BY specified on the statement.</p>
<p>If I reorganize the physical and cause the rows to occupy different physical disk positions, I will get the new row that comes first on disk.</p>
<p>I suppose a clear concept of &#8220;order&#8221; would need to be supplied. The OP probably needs to be more clear. Row number in a result set and physical record number are usually different values.</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79827</link>
		<dc:creator>carlosdl</dc:creator>
		<pubDate>Thu, 29 Jul 2010 23:17:12 +0000</pubDate>
		<guid isPermaLink="false">#comment-79827</guid>
		<description><![CDATA[&quot;&lt;i&gt;Under some of them, you can access by physical row number&lt;/i&gt;&quot;

And that involves an order as well...]]></description>
		<content:encoded><![CDATA[<p>&#8220;<i>Under some of them, you can access by physical row number</i>&#8221;</p>
<p>And that involves an order as well&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tomliotta</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79824</link>
		<dc:creator>tomliotta</dc:creator>
		<pubDate>Thu, 29 Jul 2010 21:29:47 +0000</pubDate>
		<guid isPermaLink="false">#comment-79824</guid>
		<description><![CDATA[What DBMS are you using? (DB2? MS SQL Server? MySQL? Other?) Under some of them, you can access by physical row number.

Tom]]></description>
		<content:encoded><![CDATA[<p>What DBMS are you using? (DB2? MS SQL Server? MySQL? Other?) Under some of them, you can access by physical row number.</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-13/#comment-79797</link>
		<dc:creator>carlosdl</dc:creator>
		<pubDate>Thu, 29 Jul 2010 15:00:11 +0000</pubDate>
		<guid isPermaLink="false">#comment-79797</guid>
		<description><![CDATA[To talk about &#039;nth&#039; position, there must be an order.]]></description>
		<content:encoded><![CDATA[<p>To talk about &#8216;nth&#8217; position, there must be an order.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 3/8 queries in 0.036 seconds using memcached
Object Caching 395/396 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-20 02:40:28 -->