Knowledge Guide
HomeDatabasesSQL Practice Problems

Books and Authors

Problem

Table: Books

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| book_id       | int     |
| title         | varchar |
| genre         | varchar |
+---------------+---------+
book_id is the primary key for this table.
This table contains information about the books in a library.

Table: Authors

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| author_id     | int     |
| book_id       | int     |
| author_name   | varchar |
+---------------+---------+
(author_id, book_id) is the primary key for this table.
This table contains information about the authors of the books in the library.

Problem Definition

Write a solution to find the number of books each author has written in the genre 'Fiction'.

Return the result table with author_name and the number of books in 'Fiction' genre as fiction_books_count. If an author has not written any books in the 'Fiction' genre, they should not appear in the output.

Return the result table ordered by fiction_books_count in descending order. In case of a tie, order by author_name in ascending order.

Example

Input: 
Books table:
+---------+----------------------+---------------+
| book_id | title                | genre         |
+---------+---------------=------+---------------+
| 1       | War and Peace        | Fiction       |
| 2       | Anna Karenina        | Fiction       |
| 3       | Crime and Punishment | Fiction       |
| 4       | The Great Gatsby     | Fiction       |
| 5       | Pride and Prejudice  | Romance       |
+---------+----------------------+---------------+
Authors table:
+-----------+---------+---------------------+
| author_id | book_id | author_name         |
+-----------+---------+---------------------+
| 101       | 1       | Leo Tolstoy         |
| 101       | 2       | Leo Tolstoy         |
| 102       | 3       | Fyodor Dostoevsky   |
| 103       | 4       | F. Scott Fitzgerald |
| 104       | 5       | Jane Austen         |
+-----------+---------+---------------------+
Output: 
+---------------------+---------------------+
| author_name         | fiction_books_count |
+---------------------+---------------------+
| Leo Tolstoy         | 2                   |
| F. Scott Fitzgerald | 1                   |
| Fyodor Dostoevsky   | 1                   |
+---------------------+---------------------+

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To identify the number of books each author has written in the 'Fiction' genre, we can employ SQL's JOIN, GROUP BY, and aggregation functions. The solution involves joining the Authors and Books tables, filtering for the 'Fiction' genre, counting the number of books per author, and ordering the results as specified.

SQL Query

SELECT A.author_name, COUNT(B.book_id) AS fiction_books_count FROM Authors A JOIN Books B ON A.book_id = B.book_id WHERE B.genre = 'Fiction' GROUP BY A.author_name ORDER BY fiction_books_count DESC, A.author_name ASC;

Step-by-Step Approach

Step 1: Join Authors and Books Tables and Filter for 'Fiction' Genre

Combine the Authors and Books tables to associate each author with their books, and filter the records to include only those books that belong to the 'Fiction' genre.

SQL Query:

SELECT A.author_name, B.book_id FROM Authors A JOIN Books B ON A.book_id = B.book_id WHERE B.genre = 'Fiction';

Explanation:

Output After Step 1:

Assuming the example input provided, the intermediate result after joining and filtering would be:

+---------------------+---------+ | author_name | book_id | +---------------------+---------+ | Leo Tolstoy | 1 | | Leo Tolstoy | 2 | | Fyodor Dostoevsky | 3 | | F. Scott Fitzgerald | 4 | +---------------------+---------+

Step 2: Count Fiction Books per Author

For each author, count the number of distinct 'Fiction' books they have written to determine their fiction_books_count.

SQL Query:

SELECT A.author_name, COUNT(B.book_id) AS fiction_books_count FROM Authors A JOIN Books B ON A.book_id = B.book_id WHERE B.genre = 'Fiction' GROUP BY A.author_name;

Explanation:

Output After Step 2:

Based on the intermediate result from Step 1, the aggregated counts per author would be:

+---------------------+---------------------+ | author_name | fiction_books_count | +---------------------+---------------------+ | Leo Tolstoy | 2 | | Fyodor Dostoevsky | 1 | | F. Scott Fitzgerald | 1 | +---------------------+---------------------+

Step 3: Order the Results by fiction_books_count Descending and author_name Ascending

Sort the final list of authors based on the number of 'Fiction' books they have written in descending order. In cases where authors have the same count, sort them alphabetically by their names in ascending order.

SQL Query:

ORDER BY fiction_books_count DESC, A.author_name ASC;

Explanation:

Final Output:

+---------------------+---------------------+ | author_name | fiction_books_count | +---------------------+---------------------+ | Leo Tolstoy | 2 | | F. Scott Fitzgerald | 1 | | Fyodor Dostoevsky | 1 | +---------------------+---------------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Books and Authors? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Books and Authors** (Databases) and want to truly understand it. Explain Books and Authors from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Books and Authors** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Books and Authors** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Books and Authors** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes