Bookmark Topic Watch Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

How do I limit the number of rows displayed in the results page?

Often a database query will yield hundreds or thousands of rows of data.
Trying to display all of them in one webpage could be very slow and resource intensive. Worse, it results in a poor user experience for the person who actually has to scroll through all of this data.

Well-designed websites will return a small subset of the data and provide links to move to the next or last set.

The easiest way to accomplish this is by constraining the number of rows returned from the database query in the first place.

  • MySQL uses the 'LIMIT' keyword for this.
  • PostgreSQL uses the 'LIMIT' and 'OFFSET' keywords for this.
  • Oracle uses the 'ROWNUM' keyword. It doesn't work like LIMIT and OFFSET. You will probably find it strange (this page might help).
  • DB2 uses the 'ROWNUMBER' 'OVER' combination.
  • SQL Server uses the 'TOP' keyword, followed by the number of rows from the top of the result.

  • A typical query in MySQL would look something like:

    This will yield rows 11 through 60.

    (Ref :

    Another way to limit the number of records returned is to use the

    Because this is part of the JDBC API, it works for all servers.

    It should be noted that with both approaches it is undefined which rows are returned. If you want particular rows returned, you should add an "order ..." clause so that the order of rows is defined.

    Javaranch has a forum dedicated to JDBC/SQL issues where you can go to read more about formulating SQL statements: JDBC forum

    JavaRanch Journal article on paging with JDBC and JSP

    Here are some notes from other conversations on pagination:

    Back to the JspFaq or the JdbcUsageQuestions
    Could you hold this puppy for a sec? I need to adjust this tiny ad:
    Free, earth friendly heat - from the CodeRanch trailboss
      Bookmark Topic Watch Topic
    • New Topic