Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between MySQL LIMIT range of 0,500 and 1, 500?

Tags:

sql

select

mysql

If I want in MySQL rows 1 through 500, should I use LIMIT 0, 500 or LIMIT, 1, 500? What is the difference? Thanks!

like image 863
Edward Avatar asked Jan 26 '13 10:01

Edward


People also ask

What does LIMIT 1 do in MySQL?

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set.

Does LIMIT 1 speed up query?

The answer, in short, is yes.If you limit your result to 1, then even if you are "expecting" one result, the query will be faster because your database wont look through all your records. It will simply stop once it finds a record that matches your query.

How do I LIMIT data in MySQL?

Limit Data Selections From a MySQL Database Assume we wish to select all records from 1 - 30 (inclusive) from a table called "Orders". The SQL query would then look like this: $sql = "SELECT * FROM Orders LIMIT 30"; When the SQL query above is run, it will return the first 30 records.

Why We Use LIMIT 1 in SQL?

This LIMIT clause would return 3 records in the result set with an offset of 1. What this means is that the SELECT statement would skip the first record that would normally be returned and instead return the second, third, and fourth records.


1 Answers

The first one starts from the first record of the whole result, while the second one starts on the second record of the result.

Consider the following records

ID
1 -- index of the first record is zero.
2
3
4
5
6

if you execute

LIMIT 0, 3
-- the result will be ID: 1,2,3

LIMIT 1, 3
-- the result will be ID: 2,3,4
  • SQLFiddle Demo

OTHER(s)

  • Limit - MySQL Command (for more info)
like image 62
John Woo Avatar answered Sep 28 '22 08:09

John Woo