Knowledge Guide
HomeDatabasesSQL Practice Problems

easy Library Book Checkouts by Date

Problem Statement

Table: Checkouts
This table logs each book checkout from a library, including the date of the checkout and the title of the book.

+----------------+-----------+
| Column Name    | Type      |
+----------------+-----------+
| checkout_date  | date      |
| book_title     | varchar   |
+----------------+-----------+
There is no primary key for this table, and it may contain duplicate rows.

Develop a solution to report the number of distinct book titles checked out and a lexicographically sorted list of these titles for each date.

Example

Input:

Checkouts table: +---------------+-----------------------+ | checkout_date | book_title | +---------------+-----------------------+ | 2020-07-01 | The Hobbit | | 2020-07-01 | 1984 | | 2020-07-02 | War and Peace | | 2020-07-01 | The Hobbit | | 2020-07-02 | Crime and Punishment | | 2020-07-03 | Brave New World | +---------------+-----------------------+

Output:

+---------------+---------------+----------------------------------+ | checkout_date | num_checkouts | book_titles | +---------------+---------------+----------------------------------+ | 2020-07-01 | 2 | 1984,The Hobbit | | 2020-07-02 | 2 | Crime and Punishment,War and Peace| | 2020-07-03 | 1 | Brave New World | +---------------+---------------+----------------------------------+

In this example, the report shows the number of distinct titles checked out on each date and the sorted list of these titles.

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To generate a report of distinct book titles checked out each day in a lexicographically sorted list, along with the number of distinct checkouts, we can utilize SQL's aggregation functions and window functions. The solution involves grouping the data by checkout dates, counting distinct book titles, and concatenating these titles in a sorted manner.

SQL Query

SELECT checkout_date, COUNT(DISTINCT book_title) AS num_checkouts, GROUP_CONCAT(DISTINCT book_title ORDER BY book_title ASC SEPARATOR ',') AS book_titles FROM Checkouts GROUP BY checkout_date ORDER BY checkout_date;

Step-by-Step Approach

Step 1: Group by Checkout Date and Count Distinct Book Titles

Aggregate the data by each checkout_date and count the number of unique book_titles checked out on that date.

SQL Query:

SELECT checkout_date, COUNT(DISTINCT book_title) AS num_checkouts FROM Checkouts GROUP BY checkout_date;

Explanation:

Output After Step 1:

+---------------+--------------+ | checkout_date | num_checkouts| +---------------+--------------+ | 2020-07-01 | 2 | | 2020-07-02 | 2 | | 2020-07-03 | 1 | +---------------+--------------+

Step 2: Concatenate Sorted Distinct Book Titles

Objective:
For each checkout_date, create a comma-separated list of distinct book_titles, sorted lexicographically.

SQL Query:

SELECT checkout_date, GROUP_CONCAT(DISTINCT book_title ORDER BY book_title ASC SEPARATOR ',') AS book_titles FROM Checkouts GROUP BY checkout_date;

Explanation:

Output After Step 2:

+---------------+----------------------------------+ | checkout_date | book_titles | +---------------+----------------------------------+ | 2020-07-01 | 1984,The Hobbit | | 2020-07-02 | Crime and Punishment,War and Peace| | 2020-07-03 | Brave New World | +---------------+----------------------------------+

Step 3: Combine Count and Concatenation, and Order by Checkout Date

Merge the results of Steps 1 and 2 to include both the number of distinct checkouts and the sorted list of book titles for each checkout_date. Finally, order the results by checkout_date.

SQL Query:

SELECT checkout_date, COUNT(DISTINCT book_title) AS num_checkouts, GROUP_CONCAT(DISTINCT book_title ORDER BY book_title ASC SEPARATOR ',') AS book_titles FROM Checkouts GROUP BY checkout_date ORDER BY checkout_date;

Explanation:

Final Output:

+---------------+--------------+----------------------------------+ | checkout_date | num_checkouts| book_titles | +---------------+--------------+----------------------------------+ | 2020-07-01 | 2 | 1984,The Hobbit | | 2020-07-02 | 2 | Crime and Punishment,War and Peace| | 2020-07-03 | 1 | Brave New World | +---------------+--------------+----------------------------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Library Book Checkouts by Date? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Library Book Checkouts by Date** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Library Book Checkouts by Date** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Library Book Checkouts by Date**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Library Book Checkouts by Date**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes