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 trial

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

I did not understand the question Write the SQL query to retrieve the 3rd page of results

I have difficulty understanding the request . 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. Write the SQL query to retrieve the 3rd page of results. what is the results How yo get this result to limit it to 3

1 Answer

Hi Mohammad

In your SQL statement you need to combine the keywords LIMIT and OFFSET.

I'm going to give you the correct answer so that I can walk you through the statement. Here it is:

SELECT * FROM phone_book ORDER BY last_name, first_name ASC LIMIT 20 OFFSET 40;
  • SELECT * FROM phone_book - we select all the data from the phone book
  • ORDER BY last_name, first_name ASC - we order this data by last_name and then by first_name in the ascending order. You can actually omit the word ASC as the order by default is already ascending. But to be more explicit, I tend to use ASC
  • LIMIT 20 - due to a technical limitation the phone shows 20 contacts per page
  • OFFSET 40 - we need to retrieve the third page of the phone book results and there are 20 contacts per page. To be more precise...the first page has contacts 1-20, the second page has contacts 21-40 and the third page has contacts 41-60. So in order to start showing the results from the third page we need to "ignore" the first 40 contacts so we set OFFSET to 40.

Does this help?