cswork / libraryOpen practice workspace

Coding questions · Amazon

Analyze Top Book Sales and Unique Customer Purchases

This question evaluates proficiency in data manipulation and SQL querying, focusing on aggregation, date-based filtering, joins, and set-difference logic across transactional and catalog tables.

BOOK_TRANSACTION

+---------------+------------+-------------+------+----------+
| MARKETPLACE_ID| TXN_DAY    | CUSTOMER_ID | ASIN | QUANTITY |
+---------------+------------+-------------+------+----------+
| 1             | 2025-06-01 | 10          | B001 | 2        |
| 1             | 2025-06-02 | 11          | B002 | 5        |
+---------------+------------+-------------+------+----------+

CATALOG

+---------------+------+--------------+
| MARKETPLACE_ID| ASIN | TITLE_NAME   |
+---------------+------+--------------+
| 1             | B001 | Sample Book  |
| 1             | B002 | Another Book |
+---------------+------+--------------+

MAGAZINE_TRANSACTION

+---------------+------------+-------------+------+----------+
| MARKETPLACE_ID| TXN_DAY    | CUSTOMER_ID | ASIN | QUANTITY |
+---------------+------------+-------------+------+----------+
| 1             | 2025-06-01 | 12          | M001 | 1        |
+---------------+------------+-------------+------+----------+
Scenario

E-commerce marketplace wants sales insights from book and magazine transactions.

Question

Write an SQL query to return the top 100 books (by total QUANTITY) sold in the current calendar month across all marketplaces.

Write an SQL query to list CUSTOMER_IDs that purchased at least one book but zero magazines in the entire dataset.

Hints

Use DATE_TRUNC or YEAR/MONTH filters; GROUP BY ASIN with SUM; use LEFT JOIN or NOT EXISTS between book and magazine customer sets.

Top Books in June 2025

Using BOOK_TRANSACTION and CATALOG, return the top 100 books by total quantity sold during June 2025, inclusive of 2025-06-01 and 2025-06-30. Include ASIN, TITLE_NAME, and TOTAL_QUANTITY. Join catalog titles by both marketplace and ASIN, aggregate across marketplaces by ASIN, and order by TOTAL_QUANTITY descending, then ASIN ascending.

Table BOOK_TRANSACTION

columntypeconstraints
MARKETPLACE_IDINTEGERNOT NULL
TXN_DAYDATENOT NULL
CUSTOMER_IDINTEGERNOT NULL
ASINVARCHAR(20)NOT NULL
QUANTITYINTEGERNOT NULL

Sample data:

ASINCUSTOMER_IDMARKETPLACE_IDQUANTITYTXN_DAY
B00110122025-06-01
B00211152025-06-02

Table CATALOG

columntypeconstraints
MARKETPLACE_IDINTEGERNOT NULL
ASINVARCHAR(20)NOT NULL
TITLE_NAMEVARCHAR(255)NOT NULL

Sample data:

ASINMARKETPLACE_IDTITLE_NAME
B0011Sample Book
B0021Another Book

Expected output

Top books by total quantity sold in June 2025 across all marketplaces. Based on the sample data, B002 has a higher total quantity than B001.

ASINTITLE_NAMETOTAL_QUANTITY
B002Another Book5
B001Sample Book2

Hints

  1. Use a half-open June date range: >= DATE '2025-06-01' and < DATE '2025-07-01'.
  2. Join on both MARKETPLACE_ID and ASIN.
  3. Group by ASIN and order by the summed quantity descending.

Customers Buying Books Only

List distinct CUSTOMER_IDs that purchased at least one book but zero magazines across the entire dataset, ordered by CUSTOMER_ID ascending.

Table BOOK_TRANSACTION

columntypeconstraints
MARKETPLACE_IDINTEGERNOT NULL
TXN_DAYDATENOT NULL
CUSTOMER_IDINTEGERNOT NULL
ASINVARCHAR(20)NOT NULL
QUANTITYINTEGERNOT NULL

Sample data:

ASINCUSTOMER_IDMARKETPLACE_IDQUANTITYTXN_DAY
B00110122025-06-01
B00211152025-06-02

Table MAGAZINE_TRANSACTION

columntypeconstraints
MARKETPLACE_IDINTEGERNOT NULL
TXN_DAYDATENOT NULL
CUSTOMER_IDINTEGERNOT NULL
ASINVARCHAR(20)NOT NULL
QUANTITYINTEGERNOT NULL

Sample data:

ASINCUSTOMER_IDMARKETPLACE_IDQUANTITYTXN_DAY
M00112112025-06-01

Expected output

Distinct customers who have at least one book transaction and no magazine transactions in the dataset.

CUSTOMER_ID
10
11

Hints

  1. Select distinct CUSTOMER_IDs from BOOK_TRANSACTION.
  2. Use NOT EXISTS or a left anti-join to exclude any customers that appear in MAGAZINE_TRANSACTION.