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

Zachary Wheeler
Zachary Wheeler
1,157 Points

OFFSET in SQL

In this challenge they've asked me to pull only the second page, but there are 10 books on the website alphabetically sorted.

Here is what I have but I'm not sure what's going wrong with it:

SELECT * FROM books WHERE genre = "Science Fiction" ORDER BY 10 LIMIT 2 OFFSET 10;

3 Answers

Steven Parker
Steven Parker
231,153 Points

You should not have a WHERE clause on this query.

Now that I've seen the challenge, it looks like I guessed right about the "title" column. But I don't see the challenge asking anything about a specific genre. So your query does not need a WHERE clause at all.

SELECT * FROM books ORDER BY title LIMIT 10 OFFSET 10;
Zachary Wheeler
Zachary Wheeler
1,157 Points

Ok, that worked now, Thank You!

Steven Parker
Steven Parker
231,153 Points

It looks like your last few columns got mixed up.

You have:

... ORDER BY 10 LIMIT 2 OFFSET 10;

But you probably meant something like:

... ORDER BY Title LIMIT 10 OFFSET 10;

I'm just guessing about the "Title" column since you did not link to your course page.

Zachary Wheeler
Zachary Wheeler
1,157 Points

Thanks. I put that in with those corrections and the error came back saying that I returned 0 when it asked for 10? I'm not sure what it means by that.

Here is what I put in based on your last answer:

SELECT * FROM books WHERE genre = "Science Fiction" ORDER BY title LIMIT 10 OFFSET 10;

Steven Parker
Steven Parker
231,153 Points

Can you give a link to your challenge so I can check further?

If you're being asked to return the second page and each page is defined at '10 entries', then your offset may need to be 11.

This will limit your results by 10 and start on position 11 (the first entry on page 2). Give that a shot and see if it gives you what you were looking for?

Steven Parker
Steven Parker
231,153 Points

But ... doesn't OFFSET represent the number of entries to skip? Try it yourself in the "SQL Playground" and see!