cswork / libraryOpen practice workspace

Coding questions · Amazon

Create SQL Queries for Sales and Customer Analysis

This question evaluates proficiency in SQL-based data manipulation, covering aggregation and dispersion metrics, null handling, relational joins, and window functions for ranking customers.

SALES

+----------+----------+------------+------------+----------+---------+
| sale_id  | store_id | product_id | sale_date  | quantity | revenue |
+----------+----------+------------+------------+----------+---------+
| 1        | 101      | 501        | 2024-06-01 | 3        | 75.00   |
| 2        | 102      | 502        | 2024-06-02 | NULL     | 40.00   |
| 3        | 101      | 501        | 2024-06-03 | 2        | 50.00   |
+----------+----------+------------+------------+----------+---------+

ORDERS

+----------+-------------+------------+--------------+
| order_id | customer_id | order_date | total_amount |
+----------+-------------+------------+--------------+
| 1001     | 2001        | 2024-06-01 | 120.00       |
| 1002     | 2002        | 2024-06-02 |  75.00       |
| 1003     | 2001        | 2024-06-03 |  50.00       |
+----------+-------------+------------+--------------+

ORDER_ITEMS

+----------+------------+----------+---------+
| order_id | product_id | quantity | revenue |
+----------+------------+----------+---------+
| 1001     | 501        | 2        | 50.00   |
| 1001     | 502        | 3        | 70.00   |
| 1002     | 503        | 1        | 75.00   |
+----------+------------+----------+---------+

CUSTOMERS

+-------------+--------+
| customer_id | region |
+-------------+--------+
| 2001        | West   |
| 2002        | East   |
+-------------+--------+
Scenario

Retail analytics team needs ad-hoc reports on sales performance and customer behavior.

Question

a) Given a single table SALES, write SQL to: • compute the average and standard deviation of revenue by product_id; • create a column that maps NULL quantity to 0 using CASE WHEN. b) Given three tables ORDERS, ORDER_ITEMS, CUSTOMERS, write SQL to: • join them to get total revenue per customer; • rank and dense-rank customers by total revenue within each region.

Hints

Focus on GROUP BY, JOIN conditions, COALESCE/CASE, aggregate functions, and window functions (RANK/DENSE_RANK).

Revenue stats by product

From the SALES table, compute the average and population standard deviation of revenue for each product_id.

Table SALES

columntypeconstraints
sale_idINTEGERPRIMARY KEY
store_idINTEGERNOT NULL
product_idINTEGERNOT NULL
sale_dateDATENOT NULL
quantityINTEGER
revenueDECIMAL(10,2)NOT NULL

Sample data:

product_idquantityrevenuesale_datesale_idstore_id
5013752024-06-011101
502NULL402024-06-022102
5012502024-06-033101

Expected output

Average and population standard deviation of revenue for each product_id.

avg_revenueproduct_idstddev_revenue
62.550112.5
405020

Hints

  1. Use STDDEV_POP for population standard deviation.
  2. Group the aggregation by product_id.

Map NULL quantity to 0

From the SALES table, return sale_id, quantity, and a derived column that maps NULL quantity to 0 using CASE WHEN.

Table SALES

columntypeconstraints
sale_idINTEGERPRIMARY KEY
store_idINTEGERNOT NULL
product_idINTEGERNOT NULL
sale_dateDATENOT NULL
quantityINTEGER
revenueDECIMAL(10,2)NOT NULL

Sample data:

product_idquantityrevenuesale_datesale_idstore_id
5013752024-06-011101
502NULL402024-06-022102
5012502024-06-033101

Expected output

Each sale with original quantity and quantity mapped to 0 when NULL.

quantityquantity_mappedsale_id
331
NULL02
223

Hints

  1. Use CASE WHEN ... THEN ... ELSE ... END to replace NULL.
  2. Alternatively, COALESCE(quantity, 0) also works.

Total revenue per customer and regional ranking

Given ORDERS, ORDER_ITEMS, and CUSTOMERS, write a query that: (1) joins the tables to compute total revenue per customer as the sum of ORDER_ITEMS.revenue across all their orders, and (2) ranks and dense-ranks customers by this total revenue within each region.

Table ORDERS

columntypeconstraints
order_idINTEGERPRIMARY KEY
customer_idINTEGERNOT NULL
order_dateDATENOT NULL
total_amountDECIMAL(10,2)NOT NULL

Sample data:

customer_idorder_dateorder_idtotal_amount
20012024-06-011001120
20022024-06-02100275
20012024-06-03100350

Table ORDER_ITEMS

columntypeconstraints
order_idINTEGERNOT NULL
product_idINTEGERNOT NULL
quantityINTEGERNOT NULL
revenueDECIMAL(10,2)NOT NULL

Sample data:

order_idproduct_idquantityrevenue
1001501250
1001502370
1002503175

Table CUSTOMERS

columntypeconstraints
customer_idINTEGERPRIMARY KEY
regionVARCHAR(20)NOT NULL

Sample data:

customer_idregion
2001West
2002East

Expected output

Customer total revenue (sum of ORDER_ITEMS.revenue) ranked within each region using RANK and DENSE_RANK.

customer_idregionrev_dense_rankrev_ranktotal_revenue
2002East1175
2001West11120

Hints

  1. First join CUSTOMERS to ORDERS, then LEFT JOIN ORDER_ITEMS to bring in line-item revenue.
  2. Aggregate ORDER_ITEMS.revenue by customer_id and region to get total_revenue.
  3. Use a CTE or subquery for customer totals, then apply RANK() and DENSE_RANK() partitioned by region and ordered by total_revenue DESC.