Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Yung Kazi
Yung Kazi
6,997 Points

Keyword OFFSET I am actually confused about what OFFSET is doing. [SOLVED]

My understanding is the syntax for using OFFSET is: SELECT <column> FROM <table> LIMIT <# of rows> ASC | DESC OFFSET <skipped rows> On the exercise, I used the limit as ten but then got confused on what my skipped rows would mean so I tried ten on that as well and that worked.

I want to understand why 10 worked for skipped rows and why using OFFSET is a good practice in SQL, thank you.

Showing a practice example would mean a lot.

2 Answers

Tommy Gebru
Tommy Gebru
30,150 Points

@Yung Kazi — Glad to help. You can mark a question solved by choosing a "best answer". And happy coding!

Tommy Gebru
Tommy Gebru
30,150 Points

So for this challenge, we are looking to display 10 queries at a time, with the limit by 10 statement and then of course we want to offset or begin the records on the second page of results, or rather we will exclude the first 10 :eyes:

SELECT * 
FROM books 
ORDER BY title ASC
LIMIT 10
OFFSET 10;

Therefore our sql code will select rows 11-20. This will be easier to experiment with in the SQL playground, when you can open up a recent workspace and try out the OFFSET keywords for real time results :wink: