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

Caleb Newell
Caleb Newell
5,077 Points

SQL limit

In a library database there's a books table. There's a title, author, genre and first_published column. The library database is connected to a website displaying 10 books at a time, sorted by the title alphabetically. Write a query to bring back the second page of results. Please retrieve all columns of information.

Caleb Newell
Caleb Newell
5,077 Points

what i have so far is,

SELECT * FROM books LIMIT 10 OFFSET 11;

It says I need an ORDER BY in there but I don't understand. Any help?

3 Answers

Steven Parker
Steven Parker
195,592 Points

The ORDER BY clause is what you would use to meet the requirement in the instructions to display the books "sorted by the title alphabetically".

If you need a refresher, it was covered in the Retrieving Results in a Particular Order video.

Also, if this is to be the second page of results, the OFFSET should be the same as the LIMIT.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 85,481 Points

ORDER BY is how you get your alphabetical listing of results.

You need to choose the correct column to order the data by. Also be aware that SQL is quite picky sometimes about where a keyword should go in a statement. Also, what are the values you put into the LIMIT and OFFSET keywords? Where does the cutoff happen and do they return 10 results as required?

Good luck with it. Keep trying and all this will stick eventually. :)

Saichand Pullepu
Saichand Pullepu
11,712 Points

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