Library Checkouts by Weekday and Genre
Problem
A public library tracks how many copies of each title were checked out.
Table: `Checkouts`
```text
+-------------+---------+
| Column | Type |
+-------------+---------+
| checkout_id | int |
| patron_id | int |
| taken_on | date |
| title_id | varchar |
| copies | int |
+-------------+---------+
checkout_id is the primary key. copies is the number of copies taken in that checkout.
```
Table: `Titles`
```text
+-----------+---------+
| Column | Type |
+-----------+---------+
| title_id | varchar |
| title_name| varchar |
| genre | varchar |
+-----------+---------+
title_id is the primary key.
```
For every genre, report the total number of copies checked out on each day of the week. Output one row per genre with columns `genre`, `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday`, `Saturday`, `Sunday`. A genre with no checkouts on a given day must show 0 for that day, and every genre in the catalog must appear even if it had no checkouts at all. Order the result by `genre`.
Tables
Example rows — the live problem includes the full dataset.
| checkout_id | patron_id | taken_on | title_id | copies |
|---|---|---|---|---|
| 1 | 1 | 2022-06-01 | f1 | 10 |
| 2 | 2 | 2022-06-01 | f2 | 10 |
| 3 | 3 | 2022-06-02 | f1 | 5 |
| title_id | title_name | genre |
|---|---|---|
| f1 | Tide Lines | Fiction |
| f2 | Glass Harbor | Fiction |
| c1 | Star Patrol | Comic |
Expected output
Your answer should return 4 rows with the columns genre, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
Starter code (SQL)
SELECT *
FROM Checkouts;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock