Databases Reporting with SQL Ordering, Limiting and Paging Results Paging Through Results

Amy Hsieh
Amy Hsieh
6,023 Points

Q: The page can display 20 entries, I am asked to display the 3rd page of results.

This is my ANSWER:

SELECT * FROM phone_book ORDER BY last_name ASC, first_name ASC LIMIT 60 OFFSET 40

The system replied this query will result in 60 entries. I found the system message is misleading.

SELECT * FROM phone_book ORDER BY last_name ASC,first_name ASC LIMIT 20 OFFSET 40;

Because question aksed is 3rd page output 1st page:1-20 2nd page:21-40 3rd page:41-60

So limit is 20 and the skipped rows are 40

1 Answer

Steven Parker
Steven Parker
182,565 Points

The "LIMIT" value is how many to show, not the number of the last one. So if you "LIMIT 60", you'll get 60 results, no matter what the offset is (as long is it's not within 60 of the end).