title: Row numbering in PostgreSQL
body: <p>How to get row number in PostgreSQL when the results are ordered by some column?</p>
  
  <p>e.g.</p>
  
  <pre><code>SELECT 30+row_number() AS position, * 
  FROM users 
  ORDER BY salary DESC 
  LIMIT 30 
  OFFSET 30
  </code></pre>
  
  <p>I supposed that the query would return list like this:</p>
  
  <pre><code>position | name | salary
  31       | Joy  | 4500
  32       | Katie| 4000
  33       | Frank| 3500
  </code></pre>
  
  <p>Actually i have to duplicate the <code>ORDER</code> clause into the query to make it functional:</p>
  
  <pre><code>SELECT 30+row_number(ORDER BY salary DESC) AS position, * 
  FROM users 
  ORDER BY salary DESC 
  LIMIT 30 
  OFFSET 30
  </code></pre>
  
  <p>Is there any other way how to return ordered and numbered results without necessity of duplicating the code?</p>
  
  <p>I know this can be solved by incrementing some variable in the app itself, but i wanna do this at the database layer and return to the app already numbered results...</p>