SQL · Module 13
Answers and explanations · Deliver a checked analysis capstone
Compare these references after your own attempt. You can read them at any time; they do not record a live model or vendor run.
Exercise 13a
January, February and March net sales are 1620, 1575 and 2450. Their order counts are 3, 3 and 5. Refunds follow original order month under the before-1-April cutoff, not the refund event month as a cash-flow metric.
WITH line_totals AS (
SELECT order_id,SUM(quantity*unit_price-line_discount) AS line_sales
FROM aabha_sql.order_items GROUP BY order_id
), refund_totals AS (
SELECT order_id,SUM(amount) AS refunded
FROM aabha_sql.refunds WHERE refund_date < DATE '2025-04-01' GROUP BY order_id
), order_net AS (
SELECT o.order_id,o.customer_id,o.order_date,o.channel,
l.line_sales,COALESCE(r.refunded,0) AS refunded,
l.line_sales-COALESCE(r.refunded,0) AS net_sales
FROM aabha_sql.orders AS o
JOIN line_totals AS l ON l.order_id=o.order_id
LEFT JOIN refund_totals AS r ON r.order_id=o.order_id
WHERE o.status='completed' AND o.order_date >= DATE '2025-01-01'
AND o.order_date < DATE '2025-04-01'
)
SELECT date_trunc('month',order_date)::date::text AS month,
COUNT(*) AS completed_orders,COUNT(DISTINCT customer_id) AS buying_customers,
SUM(line_sales) AS sales_before_refunds,SUM(refunded) AS refunds,SUM(net_sales) AS net_sales,
ROUND(SUM(net_sales)/NULLIF(COUNT(*),0),2) AS net_average_order_value
FROM order_net GROUP BY 1 ORDER BY month;
Expected output:
| month | completed_orders | buying_customers | sales_before_refunds | refunds | net_sales | net_average_order_value |
|---|---|---|---|---|---|---|
| 2025-01-01 | 3 | 3 | 1640.00 | 20.00 | 1620.00 | 540.00 |
| 2025-02-01 | 3 | 3 | 1650.00 | 75.00 | 1575.00 | 525.00 |
| 2025-03-01 | 5 | 5 | 2570.00 | 120.00 | 2450.00 | 490.00 |
Runnable answer: sql/solutions/13a.sql
Exercise 13b
The result retains all eight customers, including Gita and Hari at zero completed sales. A good summary distinguishes the March rise in total sales from the fall in average order value and avoids causal claims without supporting data.
WITH line_totals AS (
SELECT order_id,SUM(quantity*unit_price-line_discount) AS line_sales
FROM aabha_sql.order_items GROUP BY order_id
), refund_totals AS (
SELECT order_id,SUM(amount) AS refunded
FROM aabha_sql.refunds WHERE refund_date < DATE '2025-04-01' GROUP BY order_id
), order_net AS (
SELECT o.order_id,o.customer_id,o.order_date,o.channel,
l.line_sales,COALESCE(r.refunded,0) AS refunded,
l.line_sales-COALESCE(r.refunded,0) AS net_sales
FROM aabha_sql.orders AS o
JOIN line_totals AS l ON l.order_id=o.order_id
LEFT JOIN refund_totals AS r ON r.order_id=o.order_id
WHERE o.status='completed' AND o.order_date >= DATE '2025-01-01'
AND o.order_date < DATE '2025-04-01'
), totals AS (
SELECT customer_id,COUNT(*) AS completed_orders,SUM(net_sales) AS net_sales
FROM order_net GROUP BY customer_id
)
SELECT c.customer_id,c.customer_name,COALESCE(t.completed_orders,0) AS completed_orders,
COALESCE(t.net_sales,0) AS net_sales
FROM aabha_sql.customers AS c LEFT JOIN totals AS t ON t.customer_id=c.customer_id
ORDER BY net_sales DESC,c.customer_id;
Expected output:
| customer_id | customer_name | completed_orders | net_sales |
|---|---|---|---|
| 2 | Ben | 3 | 1890.00 |
| 3 | Chen | 2 | 1270.00 |
| 1 | Asha | 2 | 1100.00 |
| 4 | Divya | 2 | 575.00 |
| 5 | Eren | 1 | 450.00 |
| 6 | Farah | 1 | 360.00 |
| 7 | Gita | 0 | 0 |
| 8 | Hari | 0 | 0 |
Runnable answer: sql/solutions/13b.sql
Example capstone manager summary
The synthetic retailer recorded INR 5645 in net sales from 11 completed orders during January to March 2025. This measure uses historical line prices after total line discounts and subtracts refunds recorded before 1 April, attributed to the original order month. It excludes pending and cancelled orders, tax, shipping and costs.
Monthly net sales were INR 1620 in January, INR 1575 in February and INR 2450 in March. March exceeded February by INR 875, or 55.56%. Completed orders increased from three to five, while net average order value fell from INR 525 to INR 490. The higher total therefore coincided with more orders and a lower average per order. These are descriptive changes; the dataset does not establish why they occurred.
Ben contributed the largest customer net-sales total, INR 1890. Two customers had no completed sales, and both remain visible in the customer report. The order-grain result reconciles INR 5860 before refunds minus INR 215 in refunds to the net total, without joining raw payment and line records together.
A useful next question is whether the March product mix explains the lower average order value. Campaign effectiveness or profitability cannot be concluded because marketing exposure and cost data are absent. The small synthetic sample is suitable for learning and verification, not a real commercial recommendation.