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

kekai borah
kekai borah
855 Points

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

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

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,287 Points
  • For all columns use SELECT *
  • first 5 books use LIMIT clause
  • from Fantasy genre filter genre with WHERE clause
  • order by year released ORDER BY first_published
SELECT * FROM books WHERE genre = 'Fantasy' ORDER BY first_published LIMIT 5

I believe LIMIT isn't supported by MSAccess or SQL Server - for those you can use:

SELECT TOP 5 * FROM books WHERE genre = 'Fantasy' ORDER BY first_published