0% completed
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
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.
- Join Authors and Books Tables: Combine the
Authors
andBooks
tables based on thebook_id
to associate each author with their respective books. - Filter for 'Fiction' Genre: Restrict the dataset to include only those books that belong to the 'Fiction' genre.
- Count Fiction Books per Author: For each author, count the number of distinct 'Fiction' books they have written.
- Order the Results: Sort the final output by
fiction_books_count
in descending order. In case of a tie, sort byauthor_name
in ascending order.
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:
SELECT A.author_name, B.book_id
:- Retrieves the
author_name
from theAuthors
table and the correspondingbook_id
from theBooks
table.
- Retrieves the
FROM Authors A JOIN Books B ON A.book_id = B.book_id
:- Performs an inner join between the
Authors
andBooks
tables based on thebook_id
, ensuring that only matching records are combined.
- Performs an inner join between the
WHERE B.genre = 'Fiction'
:- Filters the joined records to include only those books that are categorized under the 'Fiction' genre.
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:
SELECT A.author_name, COUNT(B.book_id) AS fiction_books_count
:-
Selects each
author_name
and counts the number of associatedbook_id
s that are in the 'Fiction' genre.
-
GROUP BY A.author_name
:- Groups the results by
author_name
to perform the count for each individual author.
- Groups the results by
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:
ORDER BY fiction_books_count DESC, A.author_name ASC
:fiction_books_count DESC
:- Orders the authors by their
fiction_books_count
in descending order, placing authors with more 'Fiction' books at the top.
- Orders the authors by their
A.author_name ASC
:- For authors with the same
fiction_books_count
, orders them alphabetically byauthor_name
in ascending order.
- For authors with the same
Final Output:
+---------------------+---------------------+ | author_name | fiction_books_count | +---------------------+---------------------+ | Leo Tolstoy | 2 | | F. Scott Fitzgerald | 1 | | Fyodor Dostoevsky | 1 | +---------------------+---------------------+
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible