title: Oracle SELECT TOP 10 records
body: <p>I have an big problem with an SQL Statement in Oracle. I want to select the TOP 10 Records ordered by STORAGE_DB which aren't in a list from an other select statement.</p>
  
  <p>This one works fine for all records:</p>
  
  <pre><code>SELECT DISTINCT 
    APP_ID, 
    NAME, 
    STORAGE_GB, 
    HISTORY_CREATED, 
    TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') AS HISTORY_DATE  
    FROM HISTORY WHERE 
        STORAGE_GB IS NOT NULL AND 
          APP_ID NOT IN (SELECT APP_ID
                         FROM HISTORY
                          WHERE TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') = '06.02.2009') 
  </code></pre>
  
  <p>But when I am adding </p>
  
  <pre><code>AND ROWNUM &lt;= 10
  ORDER BY STORAGE_GB DESC
  </code></pre>
  
  <p>I'm getting some kind of "random" Records. I think because the limit takes in place before the order.</p>
  
  <p>Does someone has an good solution? The other problem: This query is realy slow (10k+ records)</p>