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 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,121 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