Which rows are returned when using LIMIT with OFFSET in MySQL?

Mysql

Mysql Problem Overview


In the query below:

SELECT column 
FROM table
LIMIT 18 OFFSET 8

how many results will we get as output and from where to where?

Mysql Solutions


Solution 1 - Mysql

It will return 18 results starting on record #9 and finishing on record #26.

Start by reading the query from offset. First you offset by 8, which means you skip the first 8 results of the query. Then you limit by 18. Which means you consider records 9, 10, 11, 12, 13, 14, 15, 16....24, 25, 26 which are a total of 18 records.

Check this out.

And also the official documentation.

Solution 2 - Mysql

OFFSET is nothing but a keyword to indicate starting cursor in table

SELECT column FROM table LIMIT 18 OFFSET 8 -- fetch 18 records, begin with record 9 (OFFSET 8)

you would get the same result form

SELECT column FROM table LIMIT 8, 18

visual representation (R is one record in the table in some order)

 OFFSET        LIMIT          rest of the table
 __||__   _______||_______   __||__
/      \ /                \ /
RRRRRRRR RRRRRRRRRRRRRRRRRR RRRR...
         \________________/
                 ||
             your result

Solution 3 - Mysql

You will get output from column value 9 to 26 as you have mentioned OFFSET as 8

Solution 4 - Mysql

Offset is majorly used to support pagination in MySql SELECT statements. First the query will execute and then the records after the offset will be returned.

For example: let's say you want to show 10 reviews per page for a product as per the order of ratings (highest first), then below query can be used to get the reviews which will be shown on third page:

Select * from Reviews where productid= order by ratings desc LIMIT 10 OFFSET 20.

Solution 5 - Mysql

It will skip first 8 records and desplays records from 9 to 26

limit 18 : Display/select 18 records

offset 8 : will skip 8 records

if Your table has ids like 1,2,3,4,5,6,7,8,9,10,11.... and so on , so 1 to 8 records will be skipped and records after 9 to 26 ie 18 records will be displayed/selected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArun KilluView Question on Stackoverflow
Solution 1 - MysqlMosty MostachoView Answer on Stackoverflow
Solution 2 - MysqlvineetView Answer on Stackoverflow
Solution 3 - MysqlMahesh PatilView Answer on Stackoverflow
Solution 4 - MysqlA PaliView Answer on Stackoverflow
Solution 5 - Mysqlw.DayaView Answer on Stackoverflow