Ask Claude about this

Top Products by Revenue Analysis

Business Context

At Amazon, understanding which products generate the most revenue is critical for inventory management, marketing strategy, and business planning. This analysis identifies our highest-performing products by revenue for the month of January 2023, helping our category managers make data-driven decisions about product promotions, restocking priorities, and supplier negotiations.

Technical Requirements

The primary objective is to develop a PostgreSQL query for the following:

Task: Find the top 3 products by total revenue in the month of January 2023.

Revenue Calculation: Revenue = Product Price × Quantity Sold

Important Considerations:

  • Only include orders with 'completed' status.
  • Only include orders placed in January 2023.
  • Round the total revenue to 2 decimal places.
  • The final output should list product ID, product name, and their total revenue.
  • Sort products in descending order by total revenue, then by product name alphabetically for ties.

Database Schema

The database consists of three tables: Products, Orders, and Order_Items.

-- Table: Products
CREATE TABLE Products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(255) NOT NULL,
    price DECIMAL(10, 2) NOT NULL
);

-- Table: Orders
CREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    order_date DATE NOT NULL,
    status VARCHAR(50) -- e.g., 'completed', 'pending', 'cancelled'
);

-- Table: Order_Items
CREATE TABLE Order_Items (
    order_item_id INT PRIMARY KEY,
    order_id INT,
    product_id INT,
    quantity INT NOT NULL,
    item_price DECIMAL(10, 2), -- Price at the time of order, can differ from current product price
    FOREIGN KEY (order_id) REFERENCES Orders(order_id),
    FOREIGN KEY (product_id) REFERENCES Products(product_id)
);

Sample Data

Assume the following sample data for the tables:

Table: Products

product_id product_name price
101 Echo Dot (5th Gen) 49.99
102 Kindle Paperwhite 139.99
103 Fire TV Stick 4K 39.99
104 Anker Power Bank 25.99
105 Instant Pot Duo 89.00
106 Bose QuietComfort 45 279.00

Table: Orders

order_id order_date status
1 2023-01-05 completed
2 2023-01-10 completed
3 2023-01-12 pending
4 2023-01-15 completed
5 2023-01-20 cancelled
6 2023-01-25 completed
7 2022-12-28 completed
8 2023-02-02 completed
9 2023-01-08 completed
10 2023-01-18 completed

Table: Order_Items

order_item_id order_id product_id quantity item_price
1 1 101 2 49.99
2 1 104 1 25.99
3 2 102 1 139.99
4 2 106 1 279.00
5 3 103 1 39.99
6 4 106 2 279.00
7 4 101 3 49.99
8 5 105 1 89.00
9 6 102 2 139.99
10 6 103 5 39.99
11 7 101 1 49.99
12 8 105 2 89.00
13 9 105 3 89.00
14 10 106 1 279.00
15 10 104 2 25.99

Note on Pricing: For this problem, use the price from the Products table for revenue calculation. The item_price in Order_Items is provided for context but not used in this specific problem's revenue calculation.

Expected Output (based on sample data):

product_id product_name total_revenue
106 Bose QuietComfort 45 1116.00
102 Kindle Paperwhite 419.97
105 Instant Pot Duo 267.00

Top 3 Products by Revenue (Jan 2023)

MODERATE

Write a PostgreSQL query to find the top 3 products by total revenue generated from 'completed' orders placed in January 2023. Display the product ID, product name, and their total revenue, rounded to two decimal places, sorted by revenue in descending order (then by product name alphabetically for ties).

Your Turn! What are your thoughts on this problem or alternative ways to solve it? Share your own SQL query attempts or insights in the comments below!

Nerchuko Academy · Free DS Interview Prep