AnalystPath

Library Checkouts by Weekday and Genre

SQLHardSenior level~15 min

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.

Checkouts
checkout_idpatron_idtaken_ontitle_idcopies
112022-06-01f110
222022-06-01f210
332022-06-02f15
Titles
title_idtitle_namegenre
f1Tide LinesFiction
f2Glass HarborFiction
c1Star PatrolComic

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

Related SQL questions