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 trialSamuel Lawal
2,845 PointsReporting with SQL question
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.
PLEASE ANSWER I HAVE NO IDEA
1 Answer
Peter Vann
36,427 PointsHi Samuel!
You need to use LIMIT and OFFSET in your query.
LIMIT sets the size of the returned result set.
OFFSET essential sets how far from the beginning of the full result set that your subset will start.
So your query should read:
SELECT *
FROM phone_book
ORDER BY last_name,
first_name
LIMIT 20
OFFSET 40;
By the way, you can use a shorthand syntax, too (OFFSET value first, LIMIT value second) like this:
SELECT *
FROM phone_book
ORDER BY last_name,
first_name
LIMIT 40,
20
And, if you noticed, I like to stack my query strings for easier readability.
For more info:
https://www.w3schools.com/php/php_mysql_select_limit.asp
https://dzone.com/articles/24-rules-to-the-sql-formatting-standard#:~:text=The%20name%20of%20an%20object,a%20name%20only%20if%20necessary (scroll down to "Alignment"
I hope this helps.
Stay safe and happy coding!