KatInterview

Multi-Table Sales Analytics with Referral Tracking

SQL & DataAuto-graded · 13 test cases
MediumAuto-gradedSQL & Dataaggregationjoinsfiltering

Last updated

Tests relational data manipulation through joins, aggregations, filtering, and ranking across interconnected tables.

You're building an analytics dashboard for an online bookstore. Given four related tables as lists of rows, compute three separate metrics.

Tables

books: Each row is [book_id, title, author_id, price]

  • book_id: unique identifier
  • title: book title string
  • author_id: foreign key to authors table
  • price: list price as a float

authors: Each row is [author_id, name, personal_url]

  • author_id: unique identifier
  • name: author name string
  • personal_url: author's website URL string (may be None)

transactions: Each row is [transaction_id, book_id, customer_id, quantity, pay_method, amount]

  • transaction_id: unique identifier
  • book_id: foreign key to books table
  • customer_id: foreign key to customers table
  • quantity: number of copies purchased
  • pay_method: payment method string (e.g., "Visa", "PayPal")
  • amount: actual charged amount as a float

customers: Each row is [customer_id, name, referred_by_customer_id]

  • customer_id: unique identifier
  • name: customer name string
  • referred_by_customer_id: ID of the customer who referred this customer (None if not referred)

Task

Return a dictionary with four keys:

  1. top_payment_methods: List of up to 5 payment methods with highest total sales. Each entry is a dict with pay_method (string) and total_sales (float). Sort by total sales descending, then by method name ascending for ties.

  2. proportion_authors_zero_sales: Float representing the proportion of authors who have never sold any books. An author with no books in the books table counts as having zero sales.

  3. proportion_authors_url_contains_keyword: Float representing the proportion of authors whose personal_url contains the given keyword substring (case-sensitive). A None URL does not contain any keyword.

  4. top_referrers: List of up to 5 customers who referred others, ranked by the average list price of books their referees purchased. Each entry is a dict with customer_id (int) and average_price (float). Only include purchases by customers who were referred by someone. If a referee makes multiple purchases, count each transaction separately when computing the average. Sort by average price descending, then by customer_id ascending for ties.

Constraints

  • All tables may be empty
  • 0 ≤ number of books, authors, transactions, customers ≤ 10,000
  • book_id, author_id, customer_id, transaction_id are unique within their tables
  • Foreign key references may point to non-existent records (treat as missing data)
  • price and amount are non-negative floats
  • quantity is a positive integer
  • The keyword parameter is a non-empty string
  • When computing proportions with zero authors, return 0.0

Examples

Example 1

Input: compute_sales_metrics([[1, 'SQL 101', 10, 29.99], [2, 'Python', 11, 39.99], [3, 'ML Guide', 10, 49.99]], [[10, 'Alice', 'example.com/alice'], [11, 'Bob', 'example.com/bob_keyword'], [12, 'Carol', 'example.com/carol']], [[1001, 1, 501, 2, 'Visa', 59.98], [1002, 2, 502, 1, 'PayPal', 39.99], [1003, 3, 503, 1, 'Visa', 49.99]], [[501, 'Dan', None], [502, 'Eve', 501], [503, 'Frank', 502]], 'keyword')

Output: {'top_payment_methods': [{'pay_method': 'Visa', 'total_sales': 109.97}, {'pay_method': 'PayPal', 'total_sales': 39.99}], 'proportion_authors_zero_sales': 0.3333333333333333, 'proportion_authors_url_contains_keyword': 0.3333333333333333, 'top_referrers': [{'customer_id': 502, 'average_price': 49.99}, {'customer_id': 501, 'average_price': 39.99}]}

Hints

Hint 1

For top payment methods, aggregate by the recorded transaction amount, not price multiplied by quantity.

Hint 2

Authors with zero sales include those who have no books and those whose books never appear in transactions.

Hint 3

When computing referrer averages, use the book's list price and count each transaction separately, regardless of quantity purchased.