Top Coding Interview Question – Solved

6 Live
Oldest Book per Genre You have been provided with two tables, library_books and book_genres, containing information about books and their corresponding genres. Write an SQL query to find the oldest book (based on publication_year) in each genre. If there are multiple books with the same publication year in a genre, return the one that appears first alphabetically by title. Table description Input format Table: library_books book_id, int, Represents the unique ID of the book book_title, string, Represents the title of the book genre_ld, int, Represents the ID of the genre of the book

Asked in: No companies listed

Image of the Question

Question Image

All Testcases Passed βœ”



Passcode Image

Solution


select bg.genre_name, lb.book_title
from library_books as lb
JOIN book_genres as bg on lb.genre_id = bg.genre_id
where(lb.genre_id, lb.publication_year) IN (
// ... rest of solution available after purchase

πŸ”’ Please login to view the solution

Explanation


```
To approach the problem of identifying the oldest book in each genre using SQL, it’s essential to first fully understand the data structure and the rules for determining the correct book per genre. You are given two tables: `library_books`, which holds information about books such as their title, publication year, and genre ID, and `book_genres`, which likely contains mappings of genre IDs to genre names. The objective is to find the oldest book in each genre based on publication year, and if there are ties in publication year, select the one with the alphabetically first title.

Start by thinking about how the data is structured. The `library_books` table includes at least the following columns: book ID, book title, publication year, and genre ID. The `book_genres` table includes the genre ID and genre name. These two tables can be linked through the genre ID, allowing you to understand which book belongs to which genre name. The core of your logic will lie in grouping books by genre and selecting the oldest book within each group, resolving ties using alphabetical order.

From an SQL perspective, your task involves two key operations: grouping and ordering. For each genre, you need to group all the books that belong to it. Within each group, you then need to determine which book has the smallest (oldest) publication year. However, if two or more books share the same oldest publication year within a genre, you are required to break the tie by selecting the book that comes first alphabetically by its title.

One way to begin thinking is to start with a join. Join the `library_books` table with the `book_genres` table using the genre ID. This step helps you bring in the genre name associated with each book. Once you have a combined view of book title, genre name, and publication year, you can proceed to the logic for picking the oldest book per genre.

There are different ways to extract the oldest entry in a group in SQL. A common one involves using window functions. You can apply a `ROW_NUMBER()` function over a partition by genre, ordered by publication year ascending and book title ascending. This ordering ensures that for each genre, the oldest book (and in case of a tie, the alphabetically first title) is ranked first. After that, simply select rows where the row number is 1 for each genre group. This will give you exactly one book per genre that satisfies both conditions.

An alternative way to think about the solution, especially if window functions are not preferred or supported, is to use a subquery or a common table expression (CTE). For each genre, you find the minimum publication year using a group by clause. Then, you join this result back to the original table to filter out only the books that match this minimum year. But even at that stage, you could still have multiple books per genre due to ties. So a second level of filtering is necessary, where for each genre and minimum year, you select the book with the smallest title alphabetically. This can also be accomplished by another level of ordering and limiting within each group.

Also, ensure your final selection includes the genre name and relevant book details like title and year, so that the output is meaningful and understandable.

Edge cases to consider in your thought process include genres with only one book, genres with multiple books all sharing the same year, and titles that are similar in spelling but differ slightly. You must ensure that your logic handles these cases without duplicates or omissions.

In summary, the key ideas in your approach are:
- Join both tables to access genre names.
- Group data by genre.
- Order within each group by publication year (ascending), then title (ascending).
- Pick the top-ranked entry per group based on that ordering.
These steps will guide you to construct an SQL query that correctly identifies the oldest book in each genre with proper tiebreakers applied.
```


Related Questions