Limiting the Number of Rows Returned

# Use the LIMIT clause to fetch the first two rows SELECT author FROM book LIMIT 2; # ... or starting from the second row, fetch two # rows SELECT author FROM book LIMIT 1, 2;

The LIMIT clause is used to fetch a limited subset of rows from a query. The clause can be used in two ways. You can either

  • Fetch the first n rows from a query, as is shown in the first example in the phrasebox. The syntax is LIMIT n, where n is some integer number.

  • Fetch a range of rows, as is shown in the second phrasebox example. The syntax for this use of LIMIT is LIMIT x, y. In this use, the clause causes the query to return y rows, starting from the xth row.


The counting of rows (at least for the purpose of LIMIT) begins with zero, not one. For example, to grab rows 12 through 15 of a query, you would say SELECT * FROM some_table LIMIT 11, 4;.

The practical uses of LIMIT will be covered in more detail in phrase "Paging Through a Result Set" in Chapter 8, "MySQL APIs."

MySQL Phrasebook. Essential Code and Commands
MySQL Phrasebook
ISBN: 0672328399
EAN: 2147483647
Year: 2003
Pages: 130

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: