cswork / libraryOpen practice workspace

Coding questions · Amazon

Derive Key Business Metrics Using SQL or Python

This question evaluates data manipulation and analytical querying skills, including joins, aggregations, conditional classification, and running-window computations in the Data Manipulation (SQL/Python) domain.

Orders

+----------+-------------+------------+---------+------------------+
| order_id | customer_id | order_date | amount  | product_category |
+----------+-------------+------------+---------+------------------+
| 1        | 101         | 2023-01-05 | 120.00 | Electronics      |
| 2        | 102         | 2023-01-06 |  55.00 | Home             |
| 3        | 101         | 2023-01-15 | 220.00 | Electronics      |
| 4        | 103         | 2023-02-01 |  80.00 | Books            |
| 5        | 102         | 2023-02-03 |  35.00 | Home             |
+----------+-------------+------------+---------+------------------+

Customers

+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 101         | Alice         |
| 102         | Bob           |
| 103         | Carol         |
| 104         | Dan           |
| 105         | Eve           |
+-------------+---------------+
Scenario

You are given two tables from an e-commerce platform and need to derive business metrics for the analytics team.

Question

For each calendar month, return the three product_category values with the highest total sales amount. Break ties by larger total sales, then alphabetically. 2. For every customer, produce a result showing order_id, order_date, amount, and that customer’s running cumulative_amount ordered by order_date. 3. Classify each order as 'High' (amount > 200), 'Medium' (amount BETWEEN 100 AND 200) or 'Low' (else). Return the count of orders in each class.

Hints

Expect to JOIN, aggregate with SUM, use CASE WHEN, and apply window functions such as ROW_NUMBER or RANK; avoid unnecessary sub-queries where possible.

Monthly top categories, customer running totals, and order classification

You are given two e-commerce tables: Orders and Customers.

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

  1. For each calendar month, return up to the three product_category values with the highest total sales amount. Break ties by total_sales descending, then product_category ascending.
  2. For every customer order, return the customer's running cumulative order amount ordered by order_date and then order_id.
  3. Classify every order amount as High when amount > 200, Medium when amount is between 100 and 200 inclusive, and Low otherwise; return the count of orders in each class.

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 Customers

columntypeconstraints
customer_idINTEGERPRIMARY KEY, NOT NULL
customer_nameVARCHAR(100)NOT NULL

Sample data:

customer_idcustomer_name
101Alice
102Bob
103Carol
104Dan
105Eve

Table Orders

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
customer_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Customers(customer_id)
order_dateDATENOT NULL
amountDECIMAL(10,2)NOT NULL
product_categoryVARCHAR(50)NOT NULL

Sample data:

amountcustomer_idorder_dateorder_idproduct_category
1201012023-01-051Electronics
551022023-01-062Home
2201012023-01-153Electronics
801032023-02-014Books
351022023-02-035Home

Expected output

Sample output for the single labeled result table.

amountamount_classcumulative_amountcustomer_idmonthorder_countorder_dateorder_idproduct_categoryresult_settotal_salesyear
NULLNULLNULLNULL1NULLNULLNULLElectronicstop_categories_by_month340.002023
NULLNULLNULLNULL1NULLNULLNULLHometop_categories_by_month55.002023
NULLNULLNULLNULL2NULLNULLNULLBookstop_categories_by_month80.002023
NULLNULLNULLNULL2NULLNULLNULLHometop_categories_by_month35.002023
120.00NULL120.00101NULLNULL2023-01-051NULLcustomer_running_totalsNULLNULL
220.00NULL340.00101NULLNULL2023-01-153NULLcustomer_running_totalsNULLNULL
55.00NULL55.00102NULLNULL2023-01-062NULLcustomer_running_totalsNULLNULL
35.00NULL90.00102NULLNULL2023-02-035NULLcustomer_running_totalsNULLNULL
80.00NULL80.00103NULLNULL2023-02-014NULLcustomer_running_totalsNULLNULL
NULLHighNULLNULLNULL1NULLNULLNULLorder_class_countsNULLNULL
NULLMediumNULLNULLNULL1NULLNULLNULLorder_class_countsNULLNULL
NULLLowNULLNULLNULL3NULLNULLNULLorder_class_countsNULLNULL

Hints

  1. Compute each requested result in its own CTE.
  2. Use ROW_NUMBER for top categories and SUM(...) OVER (...) for running totals.
  3. UNION ALL the labeled outputs into one flat result table.