Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRhea Khanna
2,838 PointsPaging with SQL, Limit/ Offset
Imagine you're developing a Contacts application on a phone. You have a database with a phone_book table. It has the columns, first_name, last_name and phone. The phone has a technical limitation to show 20 contacts on a screen at a time. Write the SQL query to retrieve the 3rd page of results from the phone_book table. Contacts are ordered by last name and then first name.
My answer: SELECT last_name, first_name FROM phone_book LIMIT 20 OFFSET 59;
I am getting this question wrong, can someone explain what I am doing wrong please
3 Answers
Erasto Oraro
5,610 PointsI think the formula for calculating the offset should be: $offset = ($page - 1) * $contacts_per_page;
In your case: offset = (3- 1) * 20 = 40 ;
Erasto Oraro
5,610 PointsSELECT last_name, first_name FROM phone_book LIMIT 20 OFFSET 40;
Rhea Khanna
2,838 PointsI haven't been taught the formula to calculate the offset the way you did but I also tried putting the answer you gave me and it still is incorrect, I have to put in the keyword ORDER BY?
Erasto Oraro
5,610 PointsSorry... I think this is close to what you are looking for.
SELECT last_name, first_name FROM phone_book ORDER BY last_name, first_name LIMIT 20 OFFSET 40;
Rhea Khanna
2,838 PointsRhea Khanna
2,838 PointsI'm still not quite sure what that means sorry