title: Find row number in a sort based on row id, then find its neighbours
body: <p>Say that I have some <code>SELECT</code> statement:</p>
  
  <pre><code>SELECT id, name FROM people
     ORDER BY name ASC;
  </code></pre>
  
  <p>I have a few million rows in the <code>people</code> table and the <code>ORDER BY</code> clause can be much more complex than what I have shown here (possibly operating on a dozen columns).</p>
  
  <p>I retrieve only a small subset of the rows (say rows 1..11) in order to display them in the UI. Now, I would like to solve following problems:</p>
  
  <ol>
  <li>Find the number of a row with a given <code>id</code>.</li>
  <li>Display the 5 items before and the 5 items after a row with a given <code>id</code>.</li>
  </ol>
  
  <p>Problem 2 is easy to solve once I have solved problem 1, as I can then use something like this if I know that the item I was looking for has row number <code>1000</code> in the sorted result set (this is the Firebird SQL dialect):</p>
  
  <pre><code>SELECT id, name FROM people
     ORDER BY name ASC
     ROWS 995 TO 1005;
  </code></pre>
  
  <p>I also know that I can find the <a href="http://www.firebirdfaq.org/faq343/" rel="nofollow">rank</a> of a row by counting all of the rows which come before the one I am looking for, but this can lead to very long <code>WHERE</code> clauses with tons of <code>OR</code> and <code>AND</code> in the condition. And I have to do this repeatedly. With my test data, this takes hundreds of milliseconds, even when using properly indexed columns, which is way too slow.</p>
  
  <p>Is there some means of achieving this by using some SQL:2003 features (such as <code>row_number</code> supported in Firebird 3.0)? I am by no way an SQL guru and I need some pointers here. Could I create a cached view where the result would include a rank/dense rank/row index?</p>