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

Sobin Sebastian
Sobin Sebastian
5,348 Points

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

We're using 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 rows.

5 Answers

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey there,

first you have to get all the books with the genre of "Fantasy" using a WHERE clause. After that you have to order them using the ORDER BY keywords by the year they were published, beginning with the oldest ones, which means that you have to use the ASC keyword in the end. In the end you should specify that you only want to have the first 5 books of the results which you can achieve by using the LIMIT keyword with the value 5.

SELECT * FROM books WHERE genre = "Fantasy" ORDER BY first_published ASC LIMIT 5;

I hope that helps! :)

Hi Tobias (or another person), Why is this version of the query incorrect? In other words, why must LIMIT 5 go at the end? I'm telling the query the exact same information, just in a different order.

SELECT * FROM books WHERE genre = "Fantasy" LIMIT 5 ORDER BY first_published ASC; 
Daniel H. Kim
Daniel H. Kim
5,333 Points

Thank you. This was helpful since it helps to understand the step by step logical process thought.

I put AND between all the conditions. Why did that not work?

SELECT * FROM books WHERE genre = "Fantasy" ORDER BY first_published LIMIT 5; You need to get rid of the ASC keyword. you can try this.I think its the right one.

Try:

SELECT * FROM books WHERE genre = "Fantasy" ORDER BY first_published ASC LIMIT 5;

Why is this quiz asking us to answer things not covered? When was it that we were told we can specify a string for the genre or the 'column'?

SELECT * FROM books WHERE genre = "Fantasy" ORDER BY genre asc, title asc LIMIT 5;