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 Limiting Results

Challenge task 1 of 2

Can anyone help with what the query should be for the below question please, I think I'm missing the point of the exercise and keep getting it wrong. Thanks.

"We're using the library database again. There's a books table. There's a title, author, genre and first_published column.

Write a query to obtain the first 5 books in the Fantasy genre ordered by the year released. Oldest first. Select all columns."

2 Answers

Here you select all columns (SELECT *); have a WHERE condition (genre = Fantasy); order by date and limit to 5 records

SELECT * FROM books WHERE genre = 'Fantasy' ORDER BY first_published LIMIT 5;
Balazs Peak
Balazs Peak
46,160 Points

I'm not sure what did you miss since you didn't provide your bad answers.

The idea is simple,

  • select each colums from the books table
  • select records only with the genre "Fantasy"
  • order them by year of publishing, in ascending order (this means oldest first in the context of date)
  • put the first 5 only in the result set (limit keyword)
select * from books where genre is "Fantasy" order by first_published asc limit 5