cswork / libraryOpen practice workspace

Coding questions · Meta

Analyze Bookstore Data to Identify Top Payment Methods

This question evaluates data manipulation and analytical SQL/Python competency, focusing on relational joins, aggregations, string filtering, ranking, and proportion calculations across multiple tables in a bookstore schema.

books

+---------+------------+----------+-------+
| book_id | title      | author_id| price |
+---------+------------+----------+-------+
| 1       | SQL 101    | 10       | 29.99 |
| 2       | Python     | 11       | 39.99 |
| 3       | ML Guide   | 10       | 49.99 |
+---------+------------+----------+-------+

authors

+-----------+-------+--------------------------+
| author_id | name  | personal_url             |
+-----------+-------+--------------------------+
| 10        | Alice | example.com/alice        |
| 11        | Bob   | example.com/bob_keyword  |
| 12        | Carol | example.com/carol        |
+-----------+-------+--------------------------+

transactions

+---------------+---------+-------------+----------+-----------+--------+
| transaction_id| book_id | customer_id | quantity | pay_method| amount |
+---------------+---------+-------------+----------+-----------+--------+
| 1001          | 1       | 501         | 2        | Visa      | 59.98  |
| 1002          | 2       | 502         | 1        | PayPal    | 39.99  |
| 1003          | 3       | 503         | 1        | Visa      | 49.99  |
+---------------+---------+-------------+----------+-----------+--------+

customers

+-------------+-------+------------------------+
| customer_id | name  | referred_by_customer_id|
+-------------+-------+------------------------+
| 501         | Dan   | NULL                   |
| 502         | Eve   | 501                    |
| 503         | Frank | 502                    |
+-------------+-------+------------------------+
Scenario

Online bookstore analytics for business insights using relational data.

Question

Using the four-table bookstore schema, write a query to return the top 5 payment methods by total sales amount. 2) Calculate (a) the proportion of authors that have zero sales and (b) the proportion of authors whose personal_url contains a given keyword (e.g., 'keyword'). 3) Some customers are referred by other customers (customers.referred_by_customer_id). Find the 5 referrers whose referees purchased books with the highest average price; ignore customers who were not referred by anyone.

Hints

Pay attention to LEFT/RIGHT joins, GROUP BY with COUNT(DISTINCT), windowing or ORDER BY + LIMIT, and handling NULL referrers.

Bookstore analytics: payments, author proportions, and referrals

You are given an online bookstore schema with books, authors, transactions, and customers.

Write one PostgreSQL query that returns a single labeled result table for these analyses:

  1. Top 5 payment methods by total sales amount.
  2. The proportion of authors with zero sales and the proportion of authors whose personal_url contains the keyword 'keyword'.
  3. For customers who were referred by another customer, the top referrers by their referred customers' quantity-weighted average purchased book price.

Return all rows in one output with a result_set column. Columns that do not apply to a given result_set should be NULL.

Table authors

columntypeconstraints
author_idINTEGERPRIMARY KEY, NOT NULL
nameVARCHAR(100)NOT NULL
personal_urlVARCHAR(255)NOT NULL

Sample data:

author_idnamepersonal_url
10Aliceexample.com/alice
11Bobexample.com/bob_keyword
12Carolexample.com/carol

Table customers

columntypeconstraints
customer_idINTEGERPRIMARY KEY, NOT NULL
nameVARCHAR(100)NOT NULL
referred_by_customer_idINTEGERFOREIGN KEY REFERENCES customers(customer_id)

Sample data:

customer_idnamereferred_by_customer_id
501DanNULL
502Eve501
503Frank502

Table books

columntypeconstraints
book_idINTEGERPRIMARY KEY, NOT NULL
titleVARCHAR(100)NOT NULL
author_idINTEGERNOT NULL, FOREIGN KEY REFERENCES authors(author_id)
priceDECIMAL(10,2)NOT NULL

Sample data:

author_idbook_idpricetitle
10129.99SQL 101
11239.99Python
10349.99ML Guide

Table transactions

columntypeconstraints
transaction_idINTEGERPRIMARY KEY, NOT NULL
book_idINTEGERNOT NULL, FOREIGN KEY REFERENCES books(book_id)
customer_idINTEGERNOT NULL, FOREIGN KEY REFERENCES customers(customer_id)
quantityINTEGERNOT NULL
pay_methodVARCHAR(20)NOT NULL
amountDECIMAL(10,2)NOT NULL

Sample data:

amountbook_idcustomer_idpay_methodquantitytransaction_id
59.981501Visa21001
39.992502PayPal11002
49.993503Visa11003

Expected output

Sample output for the single labeled result table.

avg_book_pricepay_methodreferrer_idreferrer_nameresult_settotal_salesurl_keyword_proportionzero_sales_proportion
NULLVisaNULLNULLtop_payment_methods109.97NULLNULL
NULLPayPalNULLNULLtop_payment_methods39.99NULLNULL
NULLNULLNULLNULLauthor_proportionsNULL0.33330.3333
49.99NULL502Evetop_referrers_by_avg_book_priceNULLNULLNULL
39.99NULL501Dantop_referrers_by_avg_book_priceNULLNULLNULL

Hints

  1. Use left joins from authors to preserve authors with no transactions.
  2. Weight average book price by purchased quantity.
  3. Use UNION ALL with NULL placeholders to produce one flat output.