title: How does pagination of results in databases work?
body: <p>This is a general question that applies to MySQL, Oracle DB or whatever else might be out there.</p>
  
  <p>I know for MySQL there is LIMIT offset,size; and for Oracle there is 'ROW_NUMBER' or something like that.</p>
  
  <p>But when such 'paginated' queries are called back to back, does the database engine actually do the entire 'select' all over again and then retrieve a different subset of results each time? Or does it do the overall fetching of results only once, keeps the results in memory or something, and then serves subsets of results from it for subsequent queries based on offset and size?</p>
  
  <p>If it does the full fetch every time, then it seems quite inefficient.</p>
  
  <p>If it does full fetch only once, it must be 'storing' the query somewhere somehow, so that the next time that query comes in, it knows that it has already fetched all the data and just needs to extract next page from it.
  In that case, how will the database engine handle multiple threads? Two threads executing the same query?</p>
  
  <p>I am very confused :(</p>