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 trialEdward Li
373 PointsWhat should I write the where clause?
I got error message for the below: You're missing the WHERE clauses.
when I wrote syntax below: select * from books order by first_published limit 5;
id title author genre first_published 14 Pride and Prejudice Jane Austen Classic 1813 15 Emma Jane Austen Classic 1815 10 Frankenstein Mary Shelley Horror 1818 20 Animal Farm George Orwell 1945 16 1984 George Orwell Fiction 1949
7 Answers
pooja tandan
5,225 PointsSELECT * FROM books WHERE genre ="Science Fiction" ORDER BY first_published ASC LIMIT 1;
V. T van der Vlugt
14,883 PointsYou place the WHERE clause after the FROM part like:
SELECT * FROM books WHERE condition ORDER BY first_published limit 5;
Edward Li
373 PointsT van der Vlugt, it works for me. Thanks for your quick response! Edward
I have a second question for retrieving 10 row per page. After writing the syntax, there was a bummer as below.
My question is how to retrieve 10 books per page by retrieving the earliest Science Fiction book.
Thanks!
Bummer! Your query needs to retrive the earliest 'Science Fiction' book from the books
table. Get Help Try Again
Edward Li
373 Points2 bummers I got as below, when I got 10 returns, the bummer was retrieving 'Science Fiction'. When I added genre = 'Science Fiction', I got 10 books were expected. The inquiry was order by title and limit 10 offset 2. Thanks
select * from books order by title limit 10 offset 2;
id title author genre first_published
20 Animal Farm George Orwell 1945
13 Armada Ernest Cline Science Fiction 2015
19 Contact Carl Sagan Science Fiction 1985
17 Dune Frank Herbert Science Fiction 1965
15 Emma Jane Austen Classic 1815
10 Frankenstein Mary Shelley Horror 1818
2 Harry Potter and the Chamber of Secrets J.K. Rowling Fantasy 1998
7 Harry Potter and the Deathly Hallows J.K. Rowling Fantasy 2007
4 Harry Potter and the Goblet of Fire J.K. Rowling Fantasy 2000
6 Harry Potter and the Half-Blood Prince J.K. Rowling Fantasy 2005
Bummer! Your query needs to retrive the earliest 'Science Fiction' book from the books
table.
select * from books where genre = 'Science Fiction' order by title limit 10 offset 2;
id title author genre first_published 17 Dune Frank Herbert Science Fiction 1965 12 Ready Player One Ernest Cline Science Fiction 2011 18 The Circle Dave Eggers Science Fiction 2013 11 The Martian Andy Weir Science Fiction 2014 Bummer! Ten books were expected. 4 were returned in the results.
V. T van der Vlugt
14,883 PointsCould you link the challenge for me , i'll try it out myself.
Edward Li
373 PointsV. T van der Vlugt
14,883 PointsI tried the challenge myself
the answer to question:
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.
Answer:
SELECT * FROM books
ORDER BY title
ASC limit 10 OFFSET 10;
Task 2: Imagine you're developing a Contacts application on a phone. You have a database with a phone_book table. It has the columns, first_name, last_name and phone. The phone has a technical limitation to show 20 contacts on a screen at a time. Write the SQL query to retrieve the 3rd page of results from the phone_book table. Contacts are ordered by last name and then first name.
Answer:
SELECT * FROM phone_book
ORDER BY last_name, first_name
ASC LIMIT 20 OFFSET 40;
Hope this helps
Edward Li
373 PointsBingo! you have the answers for me! Thanks for your help! Edward
V. T van der Vlugt
14,883 PointsI'd think your query would look like:
SELECT * FROM books
WHERE books.genre = 'Science Fiction'
ORDER BY first_published
ASC limit 10;
please tell if it is working or not,