title: Oracle SQL query returns same set of results despite variation in ROWNUM value
body: <p>I'm working on a Oracle/PHP application and trying to implement a "load-more" functionality.</p>
  
  <p>Here are the two tables and their structures:</p>
  
  <p><code>questions:</code>
  <code>id (int) | title (varchar(999)) | datetime (varchar(999))</code></p>
  
  <p><code>questions_tags:</code>
  <code>id (int) | question_id (int) | tag_id (int)</code></p>
  
  <p>The following is the code in index.php:</p>
  
  <pre><code>$stid = oci_parse($conn, "select * from (
                                select a.*, ROWNUM rnum from (
                                    SELECT questions.id FROM questions_tags, questions WHERE 
                                        questions.id = questions_tags.question_id AND
                                        questions_tags.tag_id IN (1,2,3,4,5,6,7) ORDER BY questions.datetime ASC
                                ) a where rownum &lt;= 10
                            ) where rnum &gt;= 0");
  oci_execute($stid);
  while (oci_fetch($stid)) {
      // Do work with the results
  }
  </code></pre>
  
  <p>The following is the javascript behind it:</p>
  
  <pre><code>$("#load_more").click(function(e) {
      $.ajax({
          url: "loadMore.php?x=" + $(".span_data:last").data("id"),
          success:function(data, textStatus, jqXHR)
          {
              if (data)
              {
                  // append data
              } else {
                  // remove button
              }
          },
          error: function(jqXHR, textStatus, errorThrown)
          {
              //if fails
          }
      });
      e.preventDefault(); //STOP default action
  });
  </code></pre>
  
  <p>Finally, here is the loadMore.php file:</p>
  
  <pre><code>&lt;?php
  $x = $_GET['x'];
  $xTen = intval($x) + 10;
  $count_id = $x;
  
  try
  {
      $lastID = 0;
      $tagIDs = implode("','", $user_tag_ids);
      $stid = oci_parse($conn, "select * from (
                                    select a.*, ROWNUM rnum from (
                                        SELECT questions.id FROM questions_tags, questions WHERE
                                            questions.id = questions_tags.question_id AND
                                            questions_tags.tag_id IN (1,2,3,4,5,6,7) ORDER by questions.datetime DESC
                                ) a where rownum &lt;= $xTen
                            ) where rnum &gt;= $x");
      oci_execute($stid);
      while (oci_fetch($stid)) {
          // Do work with the results
      }
  }
  catch(Exception $e) {
      print "Error";
  }
  ?&gt;
  </code></pre>
  
  <p>No matter what the value of <code>x</code> is, the <code>loadMore.php</code> file continues to return the same set of results. So if I do <code>/loadMore.php?x=10</code>, I will get the same results as that returned by <code>/loadMore.php?x=100</code> However, if I go even further (<code>/loadMore.php?x=1500</code>), that's when I finally get different results. I can't understand what's going wrong here.</p>
  
  <p><strong>As I said in the comments:</strong></p>
  
  <p>If I remove all the <code>rownum</code> parameters and just run </p>
  
  <pre><code>SELECT * FROM questions WHERE EXISTS (
      SELECT 1 FROM questions_tags WHERE 
          questions.id = questions_tags.question_id AND 
          questions_tags.tag_id IN (1,2,3,4,5,6,7)
  ) ORDER BY datetime DESC
  </code></pre>
  
  <p>I get all the results, probably a thousand rows. I'm trying to get the first 10 of those thousand rows and then keep getting the next 10 when the user clicks the "load more" button. That's why I have same query index.php and loadMore.php</p>