Backup
Backup
Retrieve the names and prices of all products in the 'products' table.
Get the total number of orders placed.
Show the distinct payment methods used by customers.
Fetch the order date and total amount for orders above $500
List all customers from California.
Get the names and email addresses of all customers from the 'customers' table
Show the total number of orders in the 'orders' table.
Fetch the distinct product categories from the 'products' table.
Retrieve the order date and total amount for orders over $1000 from the 'orders' table.
List all customers from the 'customers' table who are located in New York
Get the product names and their prices from the 'products' table where the price is greater than $50.
Show the order IDs and shipping addresses from the 'orders' table where the order status is 'Shipped'
Fetch the customer names and phone numbers from the 'customers' table where the country is 'USA'
Retrieve the product names and stock quantities from the 'products' table where the stock is less than 10.
List all orders from the 'orders' table placed after January 1, 2024.
Get the email addresses of customers who have placed more than 5 orders from the 'customers' and 'orders' tables.
Fetch the product names and their categories for products priced between $20 and $100 from the 'products' table.
Show the total revenue from the 'orders' table
Get the most expensive product from the 'products' table.
List the top 5 most recent orders from the 'orders' table.
Get the customer names and the total amount of their orders from the 'customers' and 'orders' tables
Fetch the order ID, product name, and quantity for each product in an order from the 'orders', 'order_details', and 'products' t
Get the most popular product (the one ordered the most) and how many times it was ordered.
Fetch the customers and their most recent order date
Fetch the top 3 customers who have spent the most money.
Show the products along with the total quantity ordered and total revenue generated for each product.
Fetch the highest, lowest, and average transaction amounts from the 'orders' table.
List the customers who made the highest-value single transaction.
Get the total revenue and total number of transactions per year
Find the customers who have spent more than $5000 in total transactions.
Identify the top 5 customers with the highest lifetime value (total spending) and their most recent order date.
Find the product with the highest revenue over the past 6 months.
Get the average order value and total orders per customer, only for customers who have placed more than 3 orders.
Show the revenue trend over the past year, broken down by month.
Find the products that have been ordered less than 5 times in total.
Get the top 3 cities with the highest total revenue.
Get the most frequently ordered product along with the total quantity ordered.
List customers who ordered the same product more than once.
Get the total number of orders and total revenue for each quarter in the last 2 years.
Identify customers who placed an order for the first time in the last 30 days.
Segment customers into high, medium, and low spenders based on their total spending.
Identify overstocked products — products with high stock but low sales.
Determine the reorder point for each product — when stock is running low based on past demand
Identify customers who used to order frequently but haven’t ordered in the last 3 months.
Find VIP customers — those with high order frequency and high total spending.
Track product demand — show weekly sales trends for each product
Forecast next month’s revenue based on the last 12 months’ average growth rate.
Identify products with the highest growth in sales over the past 6 months.
Calculate the profit margin for each product based on total revenue and cost.
Find the most profitable customers (highest total profit generated from their orders).
Calculate average lead time between product orders and delivery date.
Predict top-selling products for the next quarter based on past quarterly sales trends.
Identify high-value but at-risk customers — high CLV but no recent purchases.
Create behavioral segments based on purchase patterns and average order value.
Forecast product demand for the holiday season based on last year’s holiday sales.
Estimate the impact of customer churn on future revenue.
Calculate the CLV for each customer based on their order history and average spend.
Identify customers who haven’t placed an order in the last 6 months and their total spend.
Find slow-moving products — those with low sales over the past year.
Find the median, standard deviation, and 90th percentile of order values.
Identify top and bottom 5 products by revenue and their average order value.
Calculate contribution margin and gross profit percentage for each product category.
Calculate average time between customer orders and their order frequency.
Measure average order fulfillment time and identify slowest suppliers.
Identify top and bottom 5 products by revenue and their average order value.
Calculate the average number of days between orders for each customer.
Calculate the lifespan of each customer based on their first and most recent order.
Calculate inventory turnover rate by product over the last year.
Estimate customer lifetime value based on average order value and frequency.
Predict product demand based on the moving average of sales over the last 3 months.
Calculate the return rate for each product and identify high-return items.
Identify products that need reordering based on inventory levels and sales velocity.
Identify the top 10% most profitable customers based on total revenue and frequency.
Calculate average lead time for each supplier and highlight delayed shipments.
Calculate the average revenue each customer generates over their lifetime.
Can I see a list of all my past orders with the total amount and order date?
What similar products do other customers who bought the same items purchase?
Which customers place frequent high-value orders and should be invited to a loyalty program?
Which high-value customers haven’t placed an order in the last six months?
Which products have the highest customer satisfaction based on feedback ratings?
Which products should we recommend based on similar customer purchases?
Which merchants have the highest return rates for their products?
How has the refund rate changed over the past year for each merchant?
Is there a correlation between delivery speed and customer satisfaction for each merchant?
Which product categories drive the most revenue for each merchant?
Are refund rates increasing or decreasing over time for each merchant?
Which products show the most consistent demand over the past year?
Is there a link between faster delivery times and lower refund rates?
Which customer segments spend the most and order most frequently?
Which customers are likely to generate the most value over time?
Which customers generate the highest lifetime profit?
Which customer segments drive the most profit, and how frequently do they purchase?
Which customers drive the most long-term profit for the business
How do refund rates trend over the last 12 months
Which products have the highest refund rates, indicating customer dissatisfaction
Which merchants have the highest rates of refunds and dissatisfied customers
Which customers have the highest refund impact on each merchant’s revenue
Which high-spending customers also have high refund rates per merchant
Which products contribute the most and least to each merchant's profitability
Which merchants generate the most revenue while maintaining low refund rates
How have refund rates changed over time for each merchant
Which product categories bring the highest and lowest profits for merchants
Which customer segments (based on spending and order frequency) are most valuable
Which customers haven’t placed any orders in the last six months?
Which products have the highest refund rates, and which merchants are most affected
Which customers generate the highest revenue over their lifetime with the platform
Which products are showing the fastest growth in sales over the past six months
How has each merchant’s monthly revenue evolved over the past year
How long after receiving a product do customers typically request refunds?
How much revenue has each merchant lost due to refunded orders?
How has customer satisfaction (measured by refund rate) changed month over month
Which products should be restocked based on sales velocity
Which merchants achieve the highest profit margins across their product lines
When did each customer make their first and most recent transaction?
High-Value Customers
How many orders were shipped on the same day they were placed?
How many days on average does each merchant take to deliver orders?
What’s the average time between order and delivery for each product category?
Which customers have the highest order frequency?
Which merchants sold the most products, and how much revenue did they generate?
Which customers have the highest total spend, and which merchants do they buy from?
Which customers have the fastest average delivery times across all their orders?
Which customers have made the most orders but have the lowest total spending?
What’s the total revenue and average order value for each merchant?
Which customers place the most in-transit orders?
Which merchants have the fastest average time from order to shipping?
Which customers have the highest total transaction amount over the last six months?
Which products have the highest difference between order date and shipping date?
Which products have the lowest customer ratings and highest refund rates?
Which merchants have the fastest and slowest average order fulfillment time?
Which products have the longest average time from shipping to delivery?
Which merchants have the highest number of products with late deliveries?
Which customers spend the most on products with high refund rates?
Which products have the fastest average time from order to delivery?
Which products have the fastest average time from order to delivery?
Which products have the longest time between order and shipping?
Which merchants have the widest spread of product ratings (highest rating variance)?
Which products have the highest percentage of perfect five star ratings?
Which products have the highest consistency in ratings (lowest rating variance)?
Which merchants have the most highly-rated products on average?
Which merchants have the highest number of products with perfect five star ratings?
Which product categories show the most improvement in average ratings over time?
Which products receive the most one star ratings?
Which merchants have the highest rate of low-rated products (average rating below three)?
Which products have the highest average rating growth over the past year?
Which merchants have the most consistent product ratings (lowest standard deviation)?
How do product ratings correlate with refund requests for each merchant?
Which products tend to get refunded the most despite having high ratings?
What is the average time between order placement and delivery for each product?
Which merchants maintain the highest percentage of successful deliveries without issues?
Which merchants show the fastest improvement in shipping times over the past year?
Which merchants have the highest refund rate due to damaged products?
What is the average time between order placement and shipping for each product?
Which merchants have the highest refund rate from repeat customers?
How does the number of days since the last purchase impact the likelihood of a refund?
What is the average lifetime value of customers segmented by their first purchase amount?
Which merchants see the most orders from high-value customers?
What is the total revenue generated by each merchant, ordered from highest to lowest revenue?
How many unique products does each merchant offer, ordered alphabetically by merchant name?
What is the average order value for each product category, sorted by highest average order value?
Which customers have placed the most orders, sorted by order count in descending order?
What is the average delivery time (in days) for each merchant's orders, ordered from fastest to slowest?
Which customers have the highest total spend, sorted by total amount spent?
How many refunds have each merchant received, sorted from highest to lowest?
What is the average number of days between order and shipping for each product, sorted by fastest shipping time?
What is the total number of orders and average order value for each shipping status?
Which merchants offer products priced higher than the average product price?
Which customers have spent more than the average customer spend?
Find products that have more orders than the average number of orders per product.
Which merchants have at least one product with a rating higher than the average product rating?
Identify customers who placed orders with a total amount higher than the highest individual order amount.
Which products have been refunded more times than the average refund rate per product?
Find merchants whose total revenue is higher than the average revenue across all merchants.
Which customers have more distinct products in their orders than the average number of distinct products ordered by all cust
Identify products priced higher than the most expensive product in a specific category ('Electronics').
Which merchants have more orders than the merchant with the fewest orders?
Find customers whose average order value is higher than the overall average order value.
Which products have received a higher rating than the average rating for their category?
Identify orders where the total amount is higher than the average total amount for the customer who placed the order.
Which shipping statuses occur more frequently than the average frequency of all shipping statuses?
Find the top 5 highest-grossing products based on total sales.
Identify customers who placed more orders than the average number of orders per customer.
Calculate the running total of revenue per merchant, ordered by transaction date.
Find products that contribute more than 10% of their merchant’s total revenue.
Rank customers by their total spending.
Identify merchants whose highest-priced product is above the average product price across all merchants.
Find the product with the highest average rating for each merchant
Calculate the difference between a product’s price and the average price in its category.
Find the first and last order date for each customer.
Find the top 3 customers who spent the most on orders in the last 6 months.
Calculate the average order value for each merchant and identify those exceeding the overall average.
Rank customers by their total number of orders and show only the top 10.
Calculate the moving average of order value per customer over their last 5 orders.
Identify products that have not been sold in the last 3 months.
Get the first and last transaction date for each merchant.
Query
SELECT product_name, price FROM products;
SELECT COUNT(order_id) FROM orders;
SELECT DISTINCT payment_method FROM payments;
SELECT order_date, total_amount FROM orders WHERE total_amount > 500;
SELECT customer_name, state FROM customers WHERE state = 'California';
SELECT customer_name, email FROM customers;
SELECT COUNT(order_id) FROM orders;
SELECT DISTINCT category FROM products;
SELECT order_date, total_amount FROM orders WHERE total_amount > 1000;
SELECT customer_name, city FROM customers WHERE city = 'New York';
SELECT product_name, price FROM products WHERE price > 50;
SELECT order_id, shipping_address FROM orders WHERE order_status = 'Shipped';
SELECT customer_name, phone_number FROM customers WHERE country = 'USA';
SELECT product_name, stock_quantity FROM products WHERE stock_quantity < 10;
SELECT * FROM orders WHERE order_date > '2024-01-01';
SELECT product_name, category FROM products WHERE price BETWEEN 20 AND 100;
SELECT SUM(total_amount) AS total_revenue FROM orders;
SELECT product_name, price FROM products ORDER BY price DESC LIMIT 1;
SELECT * FROM orders ORDER BY order_date DESC LIMIT 5;
SELECT c.customer_name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
SELECT p.product_name
FROM products p
LEFT JOIN order_details od ON p.product_id = od.product_id
WHERE od.order_id IS NULL;
SELECT
MAX(total_amount) AS highest_transaction,
MIN(total_amount) AS lowest_transaction,
AVG(total_amount) AS average_transaction
FROM orders;
SELECT c.customer_name, o.total_amount
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.total_amount = (SELECT MAX(total_amount) FROM orders);
SELECT p.category,
SUM(od.quantity * p.price) AS category_revenue,
(SUM(od.quantity * p.price) / (SELECT SUM(od2.quantity * p2.price)
FROM products p2
JOIN order_details od2 ON p2.product_id = od2.product_id)) *
100 AS revenue_percentage
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.category
ORDER BY revenue_percentage DESC;
SELECT c.customer_name,
SUM(o.total_amount) AS total_spent,
CASE
WHEN SUM(o.total_amount) > 10000 THEN 'High Spender'
WHEN SUM(o.total_amount) BETWEEN 5000 AND 10000 THEN 'Medium
Spender'
ELSE 'Low Spender'
END AS customer_segment
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
ORDER BY total_spent DESC;
SELECT c.customer_name,
COUNT(o.order_id) AS total_orders,
SUM(o.total_amount) AS total_spent,
CASE
WHEN COUNT(o.order_id) >= 20 THEN 'Frequent Buyer'
WHEN COUNT(o.order_id) BETWEEN 5 AND 19 THEN 'Regular Buyer'
ELSE 'Occasional Buyer'
END AS frequency_segment
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
ORDER BY total_orders DESC;
SELECT p.product_name, SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.product_name
HAVING SUM(od.quantity) < 50
ORDER BY total_sold ASC;
WITH monthly_revenue AS (
SELECT DATE_FORMAT(order_date, '%Y-%m') AS month,
SUM(total_amount) AS revenue
FROM orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
GROUP BY DATE_FORMAT(order_date, '%Y-%m')
)
SELECT ROUND(AVG(revenue) * (1 + (AVG(revenue) - MIN(revenue)) /
MIN(revenue)), 2) AS forecast_next_month_revenue
FROM monthly_revenue;
WITH product_sales AS (
SELECT p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS month,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY p.product_name, DATE_FORMAT(o.order_date, '%Y-%m')
)
SELECT product_name,
(MAX(total_sold) - MIN(total_sold)) / MIN(total_sold) * 100 AS
sales_growth_percentage
FROM product_sales
GROUP BY product_name
ORDER BY sales_growth_percentage DESC
LIMIT 5;
SELECT p.product_name,
SUM(od.quantity * p.price) AS total_revenue,
SUM(od.quantity * p.cost) AS total_cost,
(SUM(od.quantity * p.price) - SUM(od.quantity * p.cost)) AS total_profit,
ROUND((SUM(od.quantity * p.price) - SUM(od.quantity * p.cost)) /
SUM(od.quantity * p.price) * 100, 2) AS profit_margin_percentage
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name
ORDER BY profit_margin_percentage DESC;
SELECT c.customer_name,
SUM(od.quantity * (p.price - p.cost)) AS total_profit
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
GROUP BY c.customer_name
ORDER BY total_profit DESC
LIMIT 5;
SELECT p.product_name,
ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2) AS
avg_lead_time_days
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.delivery_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_lead_time_days;
WITH quarterly_sales AS (
SELECT p.product_name,
CONCAT(YEAR(o.order_date), ' Q', QUARTER(o.order_date)) AS quarter,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.product_name, CONCAT(YEAR(o.order_date), ' Q',
QUARTER(o.order_date))
)
SELECT product_name,
ROUND(AVG(total_sold) * 1.1) AS predicted_next_quarter_sales
FROM quarterly_sales
GROUP BY product_name
ORDER BY predicted_next_quarter_sales DESC
LIMIT 5;
SELECT s.supplier_name,
ROUND(AVG(DATEDIFF(po.delivery_date, po.order_date)), 2) AS
avg_delivery_time_days
FROM suppliers s
JOIN purchase_orders po ON s.supplier_id = po.supplier_id
WHERE po.delivery_date IS NOT NULL
GROUP BY s.supplier_name
ORDER BY avg_delivery_time_days ASC
LIMIT 5;
WITH rfm AS (
SELECT
c.customer_id,
c.customer_name,
DATEDIFF(CURDATE(), MAX(o.order_date)) AS recency,
COUNT(o.order_id) AS frequency,
SUM(o.total_amount) AS monetary_value
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
)
SELECT
customer_name,
CASE
WHEN recency <= 30 AND frequency >= 10 AND monetary_value >= 10000
THEN 'VIP'
WHEN recency <= 90 AND frequency >= 5 THEN 'Loyal'
WHEN recency > 180 THEN 'Churn Risk'
ELSE 'Occasional'
END AS customer_segment
FROM rfm
ORDER BY monetary_value DESC;
WITH monthly_sales AS (
SELECT
p.product_id,
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS month,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY p.product_id, p.product_name, DATE_FORMAT(o.order_date, '%Y-
%m')
)
SELECT
product_name,
ROUND(AVG(total_sold) * 1.1) AS forecast_next_month_demand
FROM monthly_sales
WHERE month >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 6 MONTH), '%Y-
%m')
GROUP BY product_name
ORDER BY forecast_next_month_demand DESC;
WITH customer_clv AS (
SELECT
c.customer_id,
c.customer_name,
AVG(o.total_amount) AS avg_order_value,
COUNT(o.order_id) / COUNT(DISTINCT YEAR(o.order_date)) AS
purchase_frequency,
DATEDIFF(MAX(o.order_date), MIN(o.order_date)) / 365 AS customer_lifespan,
MAX(o.order_date) AS last_order_date
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
)
SELECT
customer_name,
ROUND(avg_order_value * purchase_frequency * customer_lifespan, 2) AS
customer_lifetime_value,
last_order_date
FROM customer_clv
WHERE last_order_date < DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
ORDER BY customer_lifetime_value DESC;
SELECT
c.customer_name,
COUNT(o.order_id) AS total_orders,
AVG(o.total_amount) AS avg_order_value,
CASE
WHEN COUNT(o.order_id) >= 20 AND AVG(o.total_amount) >= 500 THEN 'High-
Value Frequent Buyer'
WHEN COUNT(o.order_id) >= 20 THEN 'Frequent Buyer'
WHEN AVG(o.total_amount) >= 500 THEN 'High-Value Buyer'
ELSE 'Casual Buyer'
END AS customer_segment
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
ORDER BY customer_segment;
SELECT
p.product_name,
MONTH(o.order_date) AS month,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY p.product_name, MONTH(o.order_date)
ORDER BY p.product_name, month;
WITH monthly_customers AS (
SELECT
DATE_FORMAT(o.order_date, '%Y-%m') AS month,
COUNT(DISTINCT o.customer_id) AS active_customers
FROM orders o
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
GROUP BY DATE_FORMAT(o.order_date, '%Y-%m')
)
SELECT
ROUND((SUM(active_customers) - MIN(active_customers)) /
SUM(active_customers) * 100, 2) AS retention_rate_percentage
FROM monthly_customers;
WITH customer_trends AS (
SELECT
c.customer_id,
c.customer_name,
COUNT(o.order_id) AS total_orders,
SUM(o.total_amount) AS total_spent,
AVG(o.total_amount) AS avg_order_value,
(MAX(o.total_amount) - MIN(o.total_amount)) / MIN(o.total_amount) * 100 AS
spending_growth
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
)
SELECT
customer_name,
total_orders,
total_spent,
avg_order_value,
spending_growth
FROM customer_trends
WHERE spending_growth > 50 AND total_orders >= 5
ORDER BY spending_growth DESC;
SELECT
p.product_name,
SUM(od.quantity) AS total_sold_last_holiday
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE MONTH(o.order_date) IN (11, 12)
GROUP BY p.product_name
ORDER BY total_sold_last_holiday DESC;
WITH churned_customers AS (
SELECT
c.customer_id,
c.customer_name,
SUM(o.total_amount) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING MAX(o.order_date) < DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
)
SELECT
COUNT(customer_id) AS total_churned_customers,
SUM(total_spent) AS total_revenue_lost,
ROUND(AVG(total_spent), 2) AS avg_revenue_loss_per_customer
FROM churned_customers;
WITH customer_metrics AS (
SELECT
c.customer_id,
c.customer_name,
AVG(o.total_amount) AS avg_order_value,
COUNT(o.order_id) / COUNT(DISTINCT YEAR(o.order_date)) AS
purchase_frequency,
DATEDIFF(MAX(o.order_date), MIN(o.order_date)) / 365 AS customer_lifespan
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
)
SELECT
customer_name,
ROUND(avg_order_value * purchase_frequency * customer_lifespan, 2) AS
customer_lifetime_value
FROM customer_metrics
ORDER BY customer_lifetime_value DESC
LIMIT 10;
SELECT
c.customer_id,
c.customer_name,
SUM(o.total_amount) AS total_spent,
MAX(o.order_date) AS last_order_date
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING last_order_date < DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
ORDER BY total_spent DESC;
SELECT
p.product_id,
p.product_name,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.product_id, p.product_name
ORDER BY total_sold ASC
LIMIT 10;
SELECT
p.category,
SUM(od.quantity * (p.price - p.cost)) AS total_profit,
ROUND(SUM(od.quantity * (p.price - p.cost)) / SUM(od.quantity * p.price) * 100,
2) AS profit_margin_percentage
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.category
ORDER BY profit_margin_percentage DESC;
WITH quarterly_sales AS (
SELECT
p.product_name,
CONCAT(YEAR(o.order_date), ' Q', QUARTER(o.order_date)) AS quarter,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.product_name, CONCAT(YEAR(o.order_date), ' Q',
QUARTER(o.order_date))
)
SELECT
product_name,
ROUND(AVG(total_sold) * 1.1) AS forecast_next_quarter_sales
FROM quarterly_sales
GROUP BY product_name
ORDER BY forecast_next_quarter_sales DESC
LIMIT 5;
Calculate customer retention rate and churn rate for the past 12 months.
SELECT
ROUND(MEDIAN(total_amount), 2) AS median_order_value,
ROUND(STDDEV(total_amount), 2) AS stddev_order_value,
ROUND(PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY total_amount), 2) AS
percentile_90_order_value
FROM orders;
WITH product_revenue AS (
SELECT
p.product_id,
p.product_name,
SUM(od.quantity * od.unit_price) AS total_revenue,
AVG(od.unit_price) AS avg_order_value
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_id, p.product_name
)
(
SELECT product_name, total_revenue, avg_order_value
FROM product_revenue
ORDER BY total_revenue DESC
LIMIT 5
)
UNION ALL
(
SELECT product_name, total_revenue, avg_order_value
FROM product_revenue
ORDER BY total_revenue ASC
LIMIT 5
);
WITH spend_stats AS (
SELECT
AVG(total_amount) AS avg_spend,
STDDEV(total_amount) AS stddev_spend
FROM orders
)
SELECT
c.customer_id,
c.customer_name,
SUM(o.total_amount) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
CROSS JOIN spend_stats s
GROUP BY c.customer_id, c.customer_name
HAVING SUM(o.total_amount) > (s.avg_spend + 2 * s.stddev_spend)
ORDER BY total_spent DESC;
SELECT
p.product_name,
SUM(od.quantity) AS total_sold,
AVG(p.stock_quantity) AS avg_stock,
ROUND(SUM(od.quantity) / NULLIF(AVG(p.stock_quantity), 0), 2) AS
inventory_turnover_rate
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.product_name
ORDER BY inventory_turnover_rate DESC;
SELECT
p.category,
SUM(od.quantity * (p.price - p.cost)) AS total_contribution_margin,
ROUND(SUM(od.quantity * (p.price - p.cost)) / NULLIF(SUM(od.quantity *
p.price), 0) * 100, 2) AS gross_profit_percentage
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.category
ORDER BY gross_profit_percentage DESC;
WITH order_intervals AS (
SELECT
o.customer_id,
DATEDIFF(LEAD(o.order_date) OVER (PARTITION BY o.customer_id ORDER BY
o.order_date), o.order_date) AS days_between_orders
FROM orders o
)
SELECT
c.customer_name,
ROUND(AVG(oi.days_between_orders), 2) AS avg_days_between_orders,
COUNT(o.order_id) AS total_orders
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_intervals oi ON c.customer_id = oi.customer_id
GROUP BY c.customer_name
ORDER BY avg_days_between_orders ASC;
SELECT
s.supplier_name,
AVG(DATEDIFF(o.shipped_date, o.order_date)) AS avg_fulfillment_days,
MAX(DATEDIFF(o.shipped_date, o.order_date)) AS max_fulfillment_days
FROM suppliers s
JOIN products p ON s.supplier_id = p.supplier_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY s.supplier_name
ORDER BY avg_fulfillment_days DESC;
WITH customer_metrics AS (
SELECT
c.customer_id,
c.customer_name,
AVG(o.total_amount - o.discount_amount) AS adjusted_avg_order_value,
COUNT(o.order_id) / COUNT(DISTINCT YEAR(o.order_date)) AS
purchase_frequency,
DATEDIFF(MAX(o.order_date), MIN(o.order_date)) / 365 AS customer_lifespan
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
)
SELECT
customer_name,
ROUND(adjusted_avg_order_value * purchase_frequency * customer_lifespan, 2)
AS adjusted_clv
FROM customer_metrics
ORDER BY adjusted_clv DESC;
WITH monthly_sales AS (
SELECT
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS month,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY p.product_name, DATE_FORMAT(o.order_date, '%Y-%m')
)
SELECT
product_name,
ROUND(AVG(total_sold) OVER (PARTITION BY product_name ORDER BY month
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 2) AS moving_avg_demand
FROM monthly_sales;
WITH product_revenue AS (
SELECT
p.product_id,
p.product_name,
SUM(od.quantity * od.unit_price) AS total_revenue,
AVG(od.unit_price) AS avg_order_value
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_id, p.product_name
)
(
SELECT product_name, total_revenue, avg_order_value
FROM product_revenue
ORDER BY total_revenue DESC
LIMIT 5
)
UNION ALL
(
SELECT product_name, total_revenue, avg_order_value
FROM product_revenue
ORDER BY total_revenue ASC
LIMIT 5
);
WITH order_intervals AS (
SELECT
o.customer_id,
DATEDIFF(LEAD(o.order_date) OVER (PARTITION BY o.customer_id ORDER BY
o.order_date), o.order_date) AS days_between_orders
FROM orders o
)
SELECT
o.customer_id,
ROUND(AVG(oi.days_between_orders), 2) AS avg_days_between_orders,
COUNT(o.order_id) AS total_orders
FROM orders o
JOIN order_intervals oi ON o.customer_id = oi.customer_id
GROUP BY o.customer_id
ORDER BY avg_days_between_orders ASC;
SELECT
s.supplier_name,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS
avg_fulfillment_days,
MAX(DATEDIFF(o.shipped_date, o.order_date)) AS max_fulfillment_days
FROM suppliers s
JOIN products p ON s.supplier_id = p.supplier_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY s.supplier_name
ORDER BY avg_fulfillment_days DESC;
SELECT
c.customer_id,
c.customer_name,
DATEDIFF(MAX(o.order_date), MIN(o.order_date)) AS customer_lifespan_days,
COUNT(o.order_id) AS total_orders
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
ORDER BY customer_lifespan_days DESC;
SELECT
p.product_name,
SUM(od.quantity) AS total_sold,
AVG(p.stock_quantity) AS avg_stock,
ROUND(SUM(od.quantity) / NULLIF(AVG(p.stock_quantity), 0), 2) AS
inventory_turnover_rate
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.product_name
ORDER BY inventory_turnover_rate DESC;
SELECT
c.customer_id,
c.customer_name,
MAX(o.order_date) AS last_order_date,
DATEDIFF(CURDATE(), MAX(o.order_date)) AS days_since_last_order
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING DATEDIFF(CURDATE(), MAX(o.order_date)) > 180
ORDER BY days_since_last_order DESC;
WITH customer_metrics AS (
SELECT
o.customer_id,
COUNT(o.order_id) AS total_orders,
SUM(o.total_amount) AS total_spent,
DATEDIFF(MAX(o.order_date), MIN(o.order_date)) AS customer_lifespan_days
FROM orders o
GROUP BY o.customer_id
)
SELECT
c.customer_id,
c.customer_name,
ROUND(cm.total_spent / NULLIF(cm.total_orders, 0), 2) AS avg_order_value,
ROUND(cm.total_orders / (NULLIF(cm.customer_lifespan_days, 0) / 30), 2) AS
monthly_order_frequency,
ROUND((cm.total_spent / NULLIF(cm.total_orders, 0)) * (cm.total_orders /
(NULLIF(cm.customer_lifespan_days, 0) / 30)) * 24, 2) AS estimated_clv
FROM customers c
JOIN customer_metrics cm ON c.customer_id = cm.customer_id
ORDER BY estimated_clv DESC;
WITH monthly_sales AS (
SELECT
p.product_id,
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS month,
SUM(od.quantity) AS total_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY p.product_id, p.product_name, DATE_FORMAT(o.order_date, '%Y-
%m')
)
SELECT
product_name,
month,
total_sold,
ROUND(AVG(total_sold) OVER (PARTITION BY product_name ORDER BY month
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 2) AS moving_avg_demand
FROM monthly_sales;
SELECT
p.category,
SUM(od.quantity * (p.price - p.cost)) AS total_contribution_margin,
ROUND(SUM(od.quantity * (p.price - p.cost)) / NULLIF(SUM(od.quantity *
p.price), 0) * 100, 2) AS gross_profit_percentage
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.category
ORDER BY gross_profit_percentage DESC;
SELECT
COUNT(o.order_id) AS total_orders,
SUM(CASE WHEN DATEDIFF(o.shipped_date, o.required_date) > 0 THEN 1 ELSE 0
END) AS late_orders,
ROUND(SUM(CASE WHEN DATEDIFF(o.shipped_date, o.required_date) > 0 THEN
1 ELSE 0 END) / COUNT(o.order_id) * 100, 2) AS late_order_percentage
FROM orders o;
WITH purchase_intervals AS (
SELECT
o.customer_id,
DATEDIFF(LEAD(o.order_date) OVER (PARTITION BY o.customer_id ORDER BY
o.order_date), o.order_date) AS days_between_orders
FROM orders o
)
SELECT
c.customer_id,
c.customer_name,
ROUND(AVG(pi.days_between_orders), 2) AS avg_days_between_orders,
CASE
WHEN ROUND(AVG(pi.days_between_orders), 2) <= 30 THEN 'Frequent Buyer'
WHEN ROUND(AVG(pi.days_between_orders), 2) BETWEEN 31 AND 90 THEN
'Occasional Buyer'
ELSE 'Rare Buyer'
END AS customer_segment
FROM customers c
JOIN purchase_intervals pi ON c.customer_id = pi.customer_id
GROUP BY c.customer_id, c.customer_name
ORDER BY avg_days_between_orders;
SELECT
p.product_name,
COUNT(od.order_id) AS total_orders,
SUM(CASE WHEN o.returned = 1 THEN 1 ELSE 0 END) AS returned_orders,
ROUND(SUM(CASE WHEN o.returned = 1 THEN 1 ELSE 0 END) /
COUNT(od.order_id) * 100, 2) AS return_rate_percentage
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY p.product_name
HAVING return_rate_percentage > 10
ORDER BY return_rate_percentage DESC;
WITH sales_velocity AS (
SELECT
p.product_id,
p.product_name,
SUM(od.quantity) / 30 AS daily_sales
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY p.product_id, p.product_name
)
SELECT
sv.product_name,
p.stock_quantity,
sv.daily_sales,
ROUND(p.stock_quantity / NULLIF(sv.daily_sales, 0), 2) AS
days_of_stock_remaining
FROM products p
JOIN sales_velocity sv ON p.product_id = sv.product_id
WHERE p.stock_quantity / NULLIF(sv.daily_sales, 0) < 10
ORDER BY days_of_stock_remaining;
WITH customer_revenue AS (
SELECT
o.customer_id,
SUM(o.total_amount) AS total_spent,
COUNT(o.order_id) AS total_orders
FROM orders o
GROUP BY o.customer_id
)
SELECT
c.customer_id,
c.customer_name,
cr.total_spent,
cr.total_orders,
ROUND(PERCENT_RANK() OVER (ORDER BY cr.total_spent DESC) * 100, 2) AS
profitability_percentile
FROM customers c
JOIN customer_revenue cr ON c.customer_id = cr.customer_id
WHERE ROUND(PERCENT_RANK() OVER (ORDER BY cr.total_spent DESC) * 100, 2)
>= 90
ORDER BY cr.total_spent DESC;
SELECT
s.supplier_name,
ROUND(AVG(DATEDIFF(o.received_date, o.order_date)), 2) AS
avg_lead_time_days,
SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) > 7 THEN 1 ELSE 0
END) AS delayed_shipments,
COUNT(o.order_id) AS total_shipments,
ROUND(SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) > 7 THEN 1
ELSE 0 END) / COUNT(o.order_id) * 100, 2) AS delay_percentage
FROM suppliers s
JOIN products p ON s.supplier_id = p.supplier_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY s.supplier_name
ORDER BY avg_lead_time_days DESC;
SELECT
c.customer_id,
c.customer_name,
ROUND(SUM(o.total_amount) / COUNT(DISTINCT o.order_id), 2) AS
avg_revenue_per_order,
ROUND(SUM(o.total_amount), 2) AS total_revenue
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
ORDER BY total_revenue DESC;
WITH customer_spending AS (
SELECT
o.customer_id,
SUM(o.total_amount) AS total_spent
FROM orders o
GROUP BY o.customer_id
)
SELECT
c.customer_id,
c.customer_name,
cs.total_spent
FROM customers c
JOIN customer_spending cs ON c.customer_id = cs.customer_id
WHERE cs.total_spent >= (SELECT PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER
BY total_spent) FROM customer_spending)
ORDER BY cs.total_spent DESC;
SELECT
s.supplier_name,
COUNT(o.order_id) AS total_orders,
SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) <= 7 THEN 1 ELSE 0
END) AS on_time_deliveries,
ROUND(SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) <= 7 THEN 1
ELSE 0 END) / COUNT(o.order_id) * 100, 2) AS on_time_delivery_rate
FROM suppliers s
JOIN products p ON s.supplier_id = p.supplier_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY s.supplier_name
ORDER BY on_time_delivery_rate DESC;
SELECT
c.customer_id,
c.customer_name,
MAX(o.order_date) AS last_order_date,
DATEDIFF(CURDATE(), MAX(o.order_date)) AS days_since_last_order
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING days_since_last_order > 365
ORDER BY days_since_last_order DESC;
WITH monthly_sales AS (
SELECT
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS month,
SUM(od.quantity * od.unit_price) AS total_revenue
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY p.product_name, DATE_FORMAT(o.order_date, '%Y-%m')
)
SELECT
ms.month,
ms.product_name,
ms.total_revenue
FROM (
SELECT
month,
product_name,
total_revenue,
RANK() OVER (PARTITION BY month ORDER BY total_revenue DESC) AS
revenue_rank
FROM monthly_sales
) ms
WHERE ms.revenue_rank = 1;
SELECT
o.order_id,
o.order_date,
o.total_amount
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id = {customer_id}
ORDER BY o.order_date DESC;
SELECT
p.product_name,
SUM(od.quantity) AS total_quantity
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
WHERE o.customer_id = {customer_id}
GROUP BY p.product_name
ORDER BY total_quantity DESC
LIMIT 5;
SELECT
ROUND(SUM(o.total_amount), 2) AS total_spent
FROM orders o
WHERE o.customer_id = {customer_id}
AND o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR);
SELECT
o.order_id,
o.order_date,
o.shipped_date,
o.required_date,
CASE
WHEN o.shipped_date IS NULL THEN 'Pending'
WHEN o.shipped_date <= o.required_date THEN 'On Time'
ELSE 'Delayed'
END AS delivery_status
FROM orders o
WHERE o.customer_id = {customer_id}
ORDER BY o.order_date DESC
LIMIT 10;
SELECT
o.order_id,
o.order_date,
p.product_name,
od.quantity,
od.unit_price
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
WHERE o.customer_id = {customer_id}
ORDER BY o.order_date DESC
LIMIT 1;
SELECT
p.category,
COUNT(od.order_id) AS total_orders
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
WHERE o.customer_id = {customer_id}
GROUP BY p.category
ORDER BY total_orders DESC
LIMIT 1;
SELECT
ROUND(AVG(o.total_amount), 2) AS average_order_value
FROM orders o
WHERE o.customer_id = {customer_id};
WITH order_dates AS (
SELECT
o.order_date,
LAG(o.order_date) OVER (PARTITION BY o.customer_id ORDER BY
o.order_date) AS previous_order_date
FROM orders o
WHERE o.customer_id = {customer_id}
)
SELECT
ROUND(AVG(DATEDIFF(order_date, previous_order_date)), 2) AS
avg_days_between_orders
FROM order_dates
WHERE previous_order_date IS NOT NULL;
SELECT
p2.product_name,
COUNT(DISTINCT od2.order_id) AS times_purchased
FROM order_details od1
JOIN order_details od2 ON od1.order_id = od2.order_id
JOIN products p1 ON od1.product_id = p1.product_id
JOIN products p2 ON od2.product_id = p2.product_id
WHERE od1.product_id IN (
SELECT od.product_id
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
WHERE o.customer_id = {customer_id}
)
AND p1.product_id != p2.product_id
GROUP BY p2.product_name
ORDER BY times_purchased DESC
LIMIT 5;
SELECT
DATE_FORMAT(o.order_date, '%Y-%m') AS month,
ROUND(SUM(o.total_amount), 2) AS total_spent
FROM orders o
WHERE o.customer_id = {customer_id}
GROUP BY DATE_FORMAT(o.order_date, '%Y-%m')
ORDER BY month;
WITH customer_orders AS (
SELECT
customer_id,
COUNT(order_id) AS total_orders
FROM orders
GROUP BY customer_id
)
SELECT
ROUND(SUM(CASE WHEN total_orders > 1 THEN 1 ELSE 0 END) /
COUNT(customer_id) * 100, 2) AS retention_rate
FROM customer_orders;
SELECT
c.customer_id,
c.customer_name,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(o.total_amount), 2) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING total_orders > 10 AND total_spent > 5000
ORDER BY total_spent DESC;
SELECT
c.customer_id,
c.customer_name,
ROUND(SUM(o.total_amount), 2) AS total_spent,
MAX(o.order_date) AS last_order_date,
DATEDIFF(CURDATE(), MAX(o.order_date)) AS days_since_last_order
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING total_spent > 5000 AND days_since_last_order > 180
ORDER BY days_since_last_order DESC;
SELECT
p.product_name,
ROUND(AVG(f.rating), 2) AS avg_rating,
COUNT(f.feedback_id) AS total_reviews
FROM products p
JOIN feedback f ON p.product_id = f.product_id
GROUP BY p.product_name
HAVING total_reviews > 10
ORDER BY avg_rating DESC;
SELECT
p2.product_name,
COUNT(DISTINCT od2.order_id) AS times_bought_together
FROM orders o
JOIN order_details od1 ON o.order_id = od1.order_id
JOIN order_details od2 ON o.order_id = od2.order_id
JOIN products p2 ON od2.product_id = p2.product_id
WHERE od1.product_id IN (
SELECT od.product_id
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
WHERE o.customer_id = {customer_id}
)
AND od1.product_id != od2.product_id
GROUP BY p2.product_name
ORDER BY times_bought_together DESC
LIMIT 5;
SELECT
m.merchant_name,
ROUND(AVG(f.rating), 2) AS avg_rating,
COUNT(f.feedback_id) AS total_reviews
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN feedback f ON p.product_id = f.product_id
GROUP BY m.merchant_name
HAVING total_reviews > 10
ORDER BY avg_rating DESC;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id}
GROUP BY m.merchant_name
ORDER BY total_orders DESC
LIMIT 1;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) <= 7 THEN 1 ELSE 0
END) AS on_time_deliveries,
ROUND(SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) <= 7 THEN 1
ELSE 0 END) / COUNT(o.order_id) * 100, 2) AS on_time_delivery_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name
ORDER BY on_time_delivery_rate DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id}
GROUP BY m.merchant_name
ORDER BY total_spent DESC;
SELECT
o.order_id,
o.order_date,
p.product_name,
od.quantity,
ROUND(od.unit_price * od.quantity, 2) AS total_price
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id} AND m.merchant_name =
'{merchant_name}'
ORDER BY o.order_date DESC
LIMIT 5;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id}
GROUP BY m.merchant_name
ORDER BY total_orders DESC
LIMIT 1;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.received_date, o.order_date)), 2) AS avg_delivery_days
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id} AND o.received_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_days ASC
LIMIT 1;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.received_date, o.order_date)), 2) AS avg_delivery_days
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id} AND o.received_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_days ASC
LIMIT 1;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id} AND o.order_date >= DATE_SUB(CURDATE(),
INTERVAL 6 MONTH)
GROUP BY m.merchant_name
ORDER BY total_spent DESC;
SELECT
m.merchant_name,
COUNT(DISTINCT o.customer_id) AS total_customers
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
GROUP BY m.merchant_name
ORDER BY total_customers DESC
LIMIT 5;
SELECT
m.merchant_name,
COUNT(DISTINCT p.product_id) AS total_products
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
GROUP BY m.merchant_name
ORDER BY total_products DESC
LIMIT 1;
SELECT
m.merchant_name,
ROUND(AVG(p.list_price - p.sale_price), 2) AS avg_discount,
ROUND(AVG((p.list_price - p.sale_price) / p.list_price) * 100, 2) AS
avg_discount_percentage
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
WHERE p.sale_price IS NOT NULL AND p.list_price > p.sale_price
GROUP BY m.merchant_name
ORDER BY avg_discount_percentage DESC
LIMIT 5;
SELECT
m.merchant_name,
COUNT(o.order_id) AS order_count,
ROUND(AVG(DATEDIFF(CURDATE(), o.order_date)), 2) AS
avg_days_between_orders
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id}
GROUP BY m.merchant_name
ORDER BY order_count DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_revenue
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id}
GROUP BY m.merchant_name
ORDER BY total_revenue DESC
LIMIT 1;
SELECT
m.merchant_name,
o.order_date
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id}
ORDER BY o.order_date DESC
LIMIT 1;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
SUM(CASE WHEN o.received_date <= o.required_date THEN 1 ELSE 0 END) AS
on_time_deliveries,
ROUND(SUM(CASE WHEN o.received_date <= o.required_date THEN 1 ELSE 0
END) / COUNT(o.order_id) * 100, 2) AS on_time_delivery_rate
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.customer_id = {customer_id} AND o.received_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY on_time_delivery_rate DESC;
SELECT
m.merchant_name,
ROUND(AVG(f.rating), 2) AS avg_product_rating,
COUNT(f.feedback_id) AS total_reviews
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN feedback f ON p.product_id = f.product_id
GROUP BY m.merchant_name
HAVING total_reviews > 10
ORDER BY avg_product_rating DESC
LIMIT 5;
SELECT
m.merchant_name,
COUNT(r.refund_id) AS total_refunds,
ROUND(SUM(r.refund_amount), 2) AS total_refund_value
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY total_refunds DESC;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) <= 7 THEN 1 ELSE 0
END) AS on_time_deliveries,
SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) > 7 THEN 1 ELSE 0
END) AS late_deliveries,
ROUND(SUM(CASE WHEN DATEDIFF(o.received_date, o.order_date) <= 7 THEN 1
ELSE 0 END) / COUNT(o.order_id) * 100, 2) AS on_time_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.customer_id = {customer_id}
GROUP BY m.merchant_name
ORDER BY on_time_rate DESC;
SELECT
m.merchant_name,
COUNT(r.refund_id) AS total_returns,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS return_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
HAVING total_orders > 10
ORDER BY return_rate DESC;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(r.refund_date, o.order_date)), 2) AS avg_refund_days,
COUNT(r.refund_id) AS total_refunds
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
HAVING total_refunds > 5
ORDER BY avg_refund_days ASC;
SELECT
m.merchant_name,
DATE_FORMAT(f.feedback_date, '%Y-%m') AS feedback_month,
ROUND(AVG(f.rating), 2) AS avg_rating,
COUNT(f.feedback_id) AS total_reviews
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN feedback f ON p.product_id = f.product_id
GROUP BY m.merchant_name, feedback_month
ORDER BY m.merchant_name, feedback_month;
SELECT
m.merchant_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
COUNT(r.refund_id) AS total_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY m.merchant_name, order_month
ORDER BY m.merchant_name, order_month;
SELECT
m.merchant_name,
ROUND(AVG(f.rating), 2) AS avg_rating,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
LEFT JOIN feedback f ON p.product_id = f.product_id
GROUP BY m.merchant_name
HAVING total_orders > 10
ORDER BY avg_rating DESC, refund_rate ASC;
SELECT
m.merchant_name,
COUNT(DISTINCT o.customer_id) AS total_customers,
COUNT(DISTINCT CASE WHEN o.order_date >= DATE_SUB(CURDATE(), INTERVAL
6 MONTH) THEN o.customer_id END) AS active_customers,
ROUND(COUNT(DISTINCT CASE WHEN o.order_date >= DATE_SUB(CURDATE(),
INTERVAL 6 MONTH) THEN o.customer_id END) / COUNT(DISTINCT o.customer_id)
* 100, 2) AS retention_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name
ORDER BY retention_rate DESC;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.received_date, o.order_date)), 2) AS avg_delivery_days,
ROUND(AVG(f.rating), 2) AS avg_rating,
COUNT(f.feedback_id) AS total_reviews
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
JOIN feedback f ON p.product_id = f.product_id
WHERE o.received_date IS NOT NULL
GROUP BY m.merchant_name
HAVING total_reviews > 10
ORDER BY avg_delivery_days ASC, avg_rating DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name
ORDER BY total_profit DESC;
SELECT
m.merchant_name,
SUM(od.quantity) AS total_units_sold,
SUM(p.stock_quantity) AS current_inventory,
ROUND(SUM(od.quantity) / NULLIF(SUM(p.stock_quantity), 0), 2) AS
inventory_turnover_ratio
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name
ORDER BY inventory_turnover_ratio DESC;
SELECT
m.merchant_name,
o.customer_id,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS customer_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, o.customer_id
ORDER BY customer_profit DESC;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.received_date, o.order_date)), 2) AS avg_delivery_time,
COUNT(o.order_id) AS total_deliveries
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.received_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_time ASC;
SELECT
m.merchant_name,
o.customer_id,
COUNT(o.order_id) AS order_count,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
ROUND(AVG(DATEDIFF(CURDATE(), o.order_date)), 2) AS
avg_days_between_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, o.customer_id
ORDER BY total_spent DESC, order_count DESC;
SELECT
m.merchant_name,
p.category,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_revenue
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, p.category
ORDER BY total_revenue DESC;
SELECT
m.merchant_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS monthly_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, order_month
ORDER BY m.merchant_name, order_month;
SELECT
m.merchant_name,
o.customer_id,
ROUND(SUM(od.quantity * od.unit_price), 2) AS lifetime_value,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, o.customer_id
ORDER BY lifetime_value DESC;
SELECT
m.merchant_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
COUNT(r.refund_id) AS monthly_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name, order_month
ORDER BY m.merchant_name, order_month;
SELECT
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
SUM(od.quantity) AS total_units_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.product_name, order_month
ORDER BY p.product_name, order_month;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.received_date, o.order_date)), 2) AS avg_delivery_days,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
WHERE o.received_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_days ASC, refund_rate ASC;
SELECT
o.customer_id,
COUNT(o.order_id) AS order_count,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
ROUND(AVG(DATEDIFF(CURDATE(), o.order_date)), 2) AS
avg_days_between_orders
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.customer_id
ORDER BY total_spent DESC, order_count DESC;
SELECT
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
SUM(od.quantity) AS total_units_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY p.product_name, order_month
ORDER BY p.product_name, order_month;
SELECT
m.merchant_name,
p.product_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, p.product_name
ORDER BY total_profit DESC;
SELECT
m.merchant_name,
COUNT(DISTINCT o.customer_id) AS total_customers,
COUNT(DISTINCT CASE WHEN o.order_date >= DATE_SUB(CURDATE(), INTERVAL
6 MONTH) THEN o.customer_id END) AS active_customers,
ROUND((COUNT(DISTINCT o.customer_id) - COUNT(DISTINCT CASE WHEN
o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH) THEN o.customer_id
END)) / COUNT(DISTINCT o.customer_id) * 100, 2) AS churn_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name
ORDER BY churn_rate DESC;
SELECT
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_sold_last_month,
ROUND(p.stock_quantity / NULLIF(SUM(od.quantity), 0), 2) AS
months_of_stock_remaining
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
GROUP BY p.product_name, p.stock_quantity
HAVING months_of_stock_remaining <= 1
ORDER BY months_of_stock_remaining;
SELECT
o.customer_id,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_revenue,
COUNT(o.order_id) AS total_orders,
ROUND(AVG(od.quantity * od.unit_price), 2) AS avg_order_value,
ROUND(AVG(DATEDIFF(CURDATE(), o.order_date)), 2) AS
avg_days_between_orders
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.customer_id
ORDER BY total_revenue DESC;
SELECT
o.customer_id,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS lifetime_profit,
COUNT(o.order_id) AS total_orders,
ROUND(AVG(od.quantity * (od.unit_price - p.cost_price)), 2) AS avg_order_profit
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
GROUP BY o.customer_id
ORDER BY lifetime_profit DESC;
SELECT
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_units_sold,
ROUND(p.stock_quantity / NULLIF(SUM(od.quantity), 0), 2) AS
stock_to_sales_ratio
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY p.product_name, p.stock_quantity
HAVING total_units_sold <= 10
ORDER BY stock_to_sales_ratio DESC;
SELECT
o.customer_id,
MAX(o.order_date) AS last_purchase_date,
COUNT(o.order_id) AS purchase_frequency,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.customer_id
ORDER BY total_spent DESC, purchase_frequency DESC;
SELECT
m.merchant_name,
p.product_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS product_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, p.product_name
ORDER BY product_profit DESC;
SELECT
o.customer_id,
MAX(o.order_date) AS last_order_date,
ROUND(DATEDIFF(CURDATE(), MAX(o.order_date)), 2) AS days_since_last_order,
COUNT(o.order_id) AS total_orders
FROM orders o
GROUP BY o.customer_id
HAVING days_since_last_order > 90
ORDER BY days_since_last_order DESC;
SELECT
m.merchant_name,
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
SUM(od.quantity) AS total_units_sold
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, p.product_name, order_month
ORDER BY m.merchant_name, p.product_name, order_month;
SELECT
o.customer_id,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit,
ROUND(AVG(DATEDIFF(CURDATE(), o.order_date)), 2) AS
avg_days_between_orders
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
GROUP BY o.customer_id
ORDER BY total_profit DESC, total_orders DESC;
SELECT
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
SUM(od.quantity) AS total_units_sold
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY p.product_name, order_month
ORDER BY p.product_name, order_month;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)) / COUNT(o.order_id), 2)
AS profit_per_order
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name
ORDER BY profit_per_order DESC;
SELECT
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_units_sold_last_3_months,
ROUND(SUM(od.quantity) / NULLIF(p.stock_quantity, 0), 2) AS
inventory_turnover_ratio
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
GROUP BY p.product_name, p.stock_quantity
ORDER BY inventory_turnover_ratio DESC;
SELECT
o.customer_id,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
COUNT(o.order_id) AS total_orders,
ROUND(AVG(od.quantity * od.unit_price), 2) AS avg_order_value,
ROUND(AVG(DATEDIFF(CURDATE(), o.order_date)), 2) AS
avg_days_between_orders,
ROUND(SUM(od.quantity * od.unit_price) / COUNT(o.order_id) * 12 /
NULLIF(AVG(DATEDIFF(CURDATE(), o.order_date)), 0), 2) AS
projected_annual_value
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.customer_id
ORDER BY projected_annual_value DESC;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)) /
NULLIF(COUNT(o.order_id), 0), 2) AS profit_per_order
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY refund_rate DESC;
SELECT
m.merchant_name,
COUNT(DISTINCT o.customer_id) AS total_customers,
COUNT(DISTINCT CASE WHEN o.order_date >= DATE_SUB(CURDATE(), INTERVAL
6 MONTH) THEN o.customer_id END) AS active_customers,
ROUND(COUNT(DISTINCT CASE WHEN o.order_date >= DATE_SUB(CURDATE(),
INTERVAL 6 MONTH) THEN o.customer_id END) / COUNT(DISTINCT o.customer_id)
* 100, 2) AS retention_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name
ORDER BY retention_rate DESC;
SELECT
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit
FROM order_details od
JOIN products p ON od.product_id = p.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY order_month
ORDER BY order_month;
SELECT
o.customer_id,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
ROUND(SUM(od.quantity * od.unit_price) / COUNT(o.order_id), 2) AS
avg_order_value
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.customer_id
ORDER BY total_spent DESC, total_orders DESC;
SELECT
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_units_sold_last_30_days,
ROUND(p.stock_quantity / NULLIF(SUM(od.quantity), 0), 2) AS
months_of_stock_remaining
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
GROUP BY p.product_name, p.stock_quantity
HAVING months_of_stock_remaining <= 1
ORDER BY months_of_stock_remaining;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY refund_rate ASC, total_profit DESC;
SELECT
o.customer_id,
MAX(o.order_date) AS last_order_date,
ROUND(DATEDIFF(CURDATE(), MAX(o.order_date)), 2) AS days_since_last_order,
COUNT(o.order_id) AS total_orders
FROM orders o
GROUP BY o.customer_id
HAVING days_since_last_order > 90
ORDER BY days_since_last_order DESC;
SELECT
m.merchant_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
COUNT(r.refund_id) AS total_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name, order_month
ORDER BY m.merchant_name, order_month;
SELECT
o.customer_id,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM orders o
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY o.customer_id
HAVING refund_rate > 20
ORDER BY refund_rate DESC;
SELECT
p.product_name,
COUNT(od.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(od.order_id) * 100, 2) AS refund_rate,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY p.product_name
ORDER BY refund_rate DESC, total_profit ASC;
SELECT
m.merchant_name,
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_units_sold_last_3_months,
ROUND(SUM(od.quantity) / NULLIF(p.stock_quantity, 0), 2) AS
inventory_turnover_ratio
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
GROUP BY m.merchant_name, p.product_name
ORDER BY inventory_turnover_ratio DESC;
SELECT
o.customer_id,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS
total_lifetime_profit,
COUNT(o.order_id) AS total_orders,
ROUND(AVG(od.quantity * (od.unit_price - p.cost_price)), 2) AS avg_order_profit
FROM orders o
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
GROUP BY o.customer_id
ORDER BY total_lifetime_profit DESC;
SELECT
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
COUNT(r.refund_id) AS total_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM orders o
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY order_month
ORDER BY order_month;
SELECT
p.product_name,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY p.product_name
ORDER BY refund_rate DESC
LIMIT 10;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY refund_rate DESC
LIMIT 10;
SELECT
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
COUNT(r.refund_id) AS total_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM orders o
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY order_month
ORDER BY order_month;
SELECT
o.customer_id,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM orders o
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY o.customer_id
HAVING refund_rate > 20
ORDER BY refund_rate DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS
total_profit_before_refunds,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
(od.unit_price - p.cost_price) ELSE 0 END), 2) AS total_refunded_amount,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)) - SUM(CASE WHEN
r.refund_id IS NOT NULL THEN od.quantity * (od.unit_price - p.cost_price) ELSE 0
END), 2) AS net_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY net_profit DESC;
SELECT
o.customer_id,
COUNT(r.refund_id) AS total_refunds,
MIN(o.order_date) AS first_refund_date,
MAX(o.order_date) AS last_refund_date,
ROUND(DATEDIFF(MAX(o.order_date), MIN(o.order_date)) / COUNT(r.refund_id),
2) AS avg_days_between_refunds
FROM orders o
LEFT JOIN refunds r ON o.order_id = r.order_id
WHERE r.refund_id IS NOT NULL
GROUP BY o.customer_id
HAVING total_refunds > 5
ORDER BY total_refunds DESC;
SELECT
m.merchant_name,
o.customer_id,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, o.customer_id
ORDER BY m.merchant_name, total_spent DESC;
SELECT
m.merchant_name,
o.customer_id,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_revenue,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
od.unit_price ELSE 0 END), 2) AS total_refunded,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
od.unit_price ELSE 0 END) / SUM(od.quantity * od.unit_price) * 100, 2) AS
refund_percentage
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name, o.customer_id
HAVING refund_percentage > 20
ORDER BY m.merchant_name, refund_percentage DESC;
SELECT
m.merchant_name,
o.customer_id,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS lifetime_profit,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, o.customer_id
ORDER BY m.merchant_name, lifetime_profit DESC;
SELECT
m.merchant_name,
o.customer_id,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, o.customer_id
ORDER BY m.merchant_name, total_orders DESC;
SELECT
m.merchant_name,
o.customer_id,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name, o.customer_id
HAVING total_spent > 1000 AND refund_rate > 20
ORDER BY m.merchant_name, refund_rate DESC;
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY refund_rate ASC
LIMIT 10;
SELECT
m.merchant_name,
p.product_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
SUM(od.quantity) AS total_units_sold
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY m.merchant_name, p.product_name, order_month
ORDER BY m.merchant_name, p.product_name, order_month;
SELECT
m.merchant_name,
p.product_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, p.product_name
ORDER BY m.merchant_name, total_profit DESC;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS
avg_fulfillment_days,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.shipped_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_fulfillment_days;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS
total_profit_before_refunds,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
(od.unit_price - p.cost_price) ELSE 0 END), 2) AS total_refunded_amount,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)) - SUM(CASE WHEN
r.refund_id IS NOT NULL THEN od.quantity * (od.unit_price - p.cost_price) ELSE 0
END), 2) AS net_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY net_profit DESC;
SELECT
m.merchant_name,
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_units_sold_last_6_months,
ROUND(SUM(od.quantity) / NULLIF(p.stock_quantity, 0), 2) AS
inventory_turnover_ratio
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY m.merchant_name, p.product_name
ORDER BY inventory_turnover_ratio DESC;
SELECT
m.merchant_name,
o.customer_id,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
od.unit_price ELSE 0 END), 2) AS total_refunded_amount,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
od.unit_price ELSE 0 END) / SUM(od.quantity * od.unit_price) * 100, 2) AS
refund_percentage
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name, o.customer_id
HAVING refund_percentage > 20 AND total_spent > 1000
ORDER BY m.merchant_name, refund_percentage DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_revenue,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY total_revenue DESC, refund_rate ASC;
SELECT
m.merchant_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name, order_month
ORDER BY order_month, refund_rate DESC;
SELECT
m.merchant_name,
p.category,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, p.category
ORDER BY total_profit DESC;
SELECT
m.merchant_name,
o.customer_id,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
CASE
WHEN COUNT(o.order_id) > 10 AND SUM(od.quantity * od.unit_price) > 5000
THEN 'High-Value, Frequent'
WHEN COUNT(o.order_id) > 10 THEN 'Frequent Buyer'
WHEN SUM(od.quantity * od.unit_price) > 5000 THEN 'High-Value Buyer'
ELSE 'Occasional Buyer'
END AS customer_segment
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, o.customer_id
ORDER BY m.merchant_name, total_spent DESC;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS avg_delivery_days,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.shipped_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_days DESC;
SELECT
m.merchant_name,
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_units_sold_last_6_months,
ROUND(SUM(od.quantity) / NULLIF(p.stock_quantity, 0), 2) AS
inventory_turnover_ratio
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY m.merchant_name, p.product_name
ORDER BY inventory_turnover_ratio DESC;
SELECT
c.customer_id,
c.customer_name,
MAX(o.order_date) AS last_order_date,
DATEDIFF(CURDATE(), MAX(o.order_date)) AS days_since_last_order
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING MAX(o.order_date) IS NOT NULL AND DATEDIFF(CURDATE(),
MAX(o.order_date)) > 180
ORDER BY days_since_last_order DESC;
SELECT
m.merchant_name,
p.product_name,
COUNT(od.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(od.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name, p.product_name
ORDER BY refund_rate DESC;
SELECT
c.customer_id,
c.customer_name,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS avg_delivery_time
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_details od ON o.order_id = od.order_id
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spent DESC;
SELECT
p.product_name,
COUNT(od.order_id) AS total_orders,
SUM(od.quantity) AS total_units_sold,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_revenue,
ROUND(AVG(od.quantity), 2) AS avg_quantity_per_order
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY p.product_name
ORDER BY total_units_sold DESC;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS avg_delivery_days,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.shipped_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_days ASC;
.
SELECT
m.merchant_name,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS avg_delivery_time
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
WHERE o.shipped_date IS NOT NULL
GROUP BY m.merchant_name
HAVING avg_delivery_time > 5
ORDER BY refund_rate DESC;
.
SELECT
m.merchant_name,
p.product_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit,
COUNT(od.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, p.product_name
ORDER BY total_profit DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity) / NULLIF(SUM(p.stock_quantity), 0), 2) AS
inventory_turnover_ratio,
SUM(p.stock_quantity) AS total_stock,
SUM(od.quantity) AS total_units_sold
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY m.merchant_name
ORDER BY inventory_turnover_ratio DESC;
SELECT
c.customer_id,
c.customer_name,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate,
CASE
WHEN COUNT(o.order_id) > 10 AND COUNT(r.refund_id) < 2 THEN 'Loyal and
Satisfied'
WHEN COUNT(o.order_id) > 10 THEN 'Loyal but High Refunds'
WHEN COUNT(r.refund_id) > 2 THEN 'High Refund Risk'
ELSE 'New or Occasional Buyer'
END AS customer_segment
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_details od ON o.order_id = od.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY c.customer_id, c.customer_name
ORDER BY refund_rate DESC, total_spent DESC;
SELECT
m.merchant_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
ROUND(SUM(od.quantity * od.unit_price), 2) AS monthly_revenue
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
GROUP BY m.merchant_name, order_month
ORDER BY order_month, monthly_revenue DESC;
SELECT
r.refund_id,
o.order_id,
c.customer_name,
DATEDIFF(r.refund_date, o.shipped_date) AS days_to_refund,
p.product_name,
m.merchant_name
FROM refunds r
JOIN orders o ON r.order_id = o.order_id
JOIN customers c ON o.customer_id = c.customer_id
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
JOIN merchants m ON p.merchant_id = m.merchant_id
WHERE o.shipped_date IS NOT NULL
ORDER BY days_to_refund DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_revenue,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
od.unit_price ELSE 0 END), 2) AS revenue_lost,
ROUND(SUM(CASE WHEN r.refund_id IS NOT NULL THEN od.quantity *
od.unit_price ELSE 0 END) / SUM(od.quantity * od.unit_price) * 100, 2) AS
revenue_loss_percentage
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY m.merchant_name
ORDER BY revenue_loss_percentage DESC;
SELECT
c.customer_id,
c.customer_name,
COUNT(o.order_id) AS total_orders,
ROUND(AVG(DATEDIFF(LEAD(o.order_date) OVER (PARTITION BY c.customer_id
ORDER BY o.order_date), o.order_date)), 2) AS avg_days_between_orders
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name
HAVING COUNT(o.order_id) > 1
ORDER BY avg_days_between_orders;
SELECT
m.merchant_name,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS avg_shipping_time,
COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.shipped_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_shipping_time;
SELECT
c.customer_id,
c.customer_name,
COUNT(o.order_id) AS total_orders,
ROUND(SUM(od.quantity * od.unit_price), 2) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_details od ON o.order_id = od.order_id
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spent DESC;
SELECT
p.category,
COUNT(od.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(od.order_id) * 100, 2) AS refund_rate
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY p.category
ORDER BY refund_rate DESC;
SELECT
DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,
COUNT(o.order_id) AS total_orders,
COUNT(r.refund_id) AS total_refunds,
ROUND(COUNT(r.refund_id) / COUNT(o.order_id) * 100, 2) AS refund_rate
FROM orders o
LEFT JOIN refunds r ON o.order_id = r.order_id
GROUP BY order_month
ORDER BY order_month;
SELECT
p.product_name,
p.stock_quantity,
SUM(od.quantity) AS total_units_sold,
ROUND(SUM(od.quantity) / NULLIF(p.stock_quantity, 0), 2) AS turnover_ratio
FROM products p
JOIN order_details od ON p.product_id = od.product_id
JOIN orders o ON od.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY p.product_name, p.stock_quantity
ORDER BY turnover_ratio DESC;
SELECT
m.merchant_name,
ROUND(SUM(od.quantity * (od.unit_price - p.cost_price)), 2) AS total_profit,
ROUND(AVG(od.unit_price - p.cost_price), 2) AS avg_profit_margin
FROM merchants m
JOIN products p ON m.merchant_id = p.merchant_id
JOIN order_details od ON p.product_id = od.product_id
GROUP BY m.merchant_name
ORDER BY total_profit DESC;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN o.delivery_status = 'Delivered' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS on_time_rate
FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
GROUP BY m.merchant_name
ORDER BY on_time_rate DESC;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_rate
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name;
SELECT c.customer_name, ROUND(SUM(o.refund_amount), 2) AS
total_refund_amount
FROM orders o
JOIN customers c USING(customer_id)
WHERE o.refund_status = 'Refunded'
GROUP BY c.customer_name;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN o.delivery_status = 'Delayed' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS late_delivery_rate
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name;
SELECT m.merchant_name,
ROUND(AVG(DATEDIFF(o.delivery_date, o.shipped_date)), 2) AS
avg_shipping_time
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
WHERE o.delivery_date IS NOT NULL AND o.shipped_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_shipping_time;
SELECT customer_id, customer_name, COUNT(order_id) AS total_orders
FROM orders
GROUP BY customer_id, customer_name
HAVING total_orders > 10;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN o.delivery_status = 'Delayed' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS delay_percentage
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name
ORDER BY delay_percentage DESC;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN DATEDIFF(o.delivery_date, o.order_date) <= 5 THEN
1 ELSE 0 END) / COUNT(o.order_id) * 100, 2) AS on_time_percentage
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY m.merchant_name;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN DATEDIFF(o.delivery_date, o.order_date) > 5 THEN 1
ELSE 0 END) / COUNT(o.order_id) * 100, 2) AS delayed_delivery_percentage
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY delayed_delivery_percentage DESC;
SELECT p.category,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_rate
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.category
ORDER BY refund_rate DESC;
SELECT m.merchant_name,
ROUND(SUM(t.transaction_amount), 2) AS total_revenue,
ROUND(AVG(t.transaction_amount), 2) AS avg_order_value
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY m.merchant_name
ORDER BY total_revenue DESC;
SELECT c.customer_id, c.customer_name, COUNT(o.order_id) AS in_transit_orders
FROM customers c
JOIN orders o USING(customer_id)
WHERE o.delivery_status = 'In Transit'
GROUP BY c.customer_id, c.customer_name
ORDER BY in_transit_orders DESC;
SELECT p.category,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_rate
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.category
ORDER BY refund_rate DESC;
SELECT m.merchant_name,
ROUND(SUM(t.transaction_amount), 2) AS total_revenue,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN
t.transaction_amount ELSE 0 END), 2) AS total_refunds
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY m.merchant_name
ORDER BY total_revenue DESC;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS return_rate
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name
ORDER BY return_rate DESC;
SELECT p.product_name,
ROUND(AVG(o.product_rating), 2) AS avg_rating,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_rate
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY refund_rate DESC, avg_rating;
SELECT m.merchant_name,
ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS
avg_fulfillment_time
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_fulfillment_time;
SELECT p.product_name,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_rate
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
ORDER BY refund_rate DESC;
SELECT p.product_name, ROUND(SUM(t.transaction_amount), 2) AS total_revenue
FROM products p
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY p.product_name
ORDER BY total_revenue DESC;
SELECT p.product_name,
ROUND(SUM(CASE WHEN o.delivery_status = 'On Time' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS on_time_percentage
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
ORDER BY on_time_percentage DESC;
SELECT p.product_name,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
ORDER BY refund_percentage DESC;
SELECT p.product_name, ROUND(AVG(DATEDIFF(o.delivery_date, o.shipped_date)),
2) AS avg_shipping_to_delivery
FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date IS NOT NULL AND o.shipped_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_shipping_to_delivery;
Which products experience the longest time between order and refund?
SELECT p.product_name,
ROUND(SUM(CASE WHEN o.delivery_status = 'Late' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS late_delivery_rate
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
ORDER BY late_delivery_rate DESC;
SELECT p.category,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS return_rate
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.category
ORDER BY return_rate;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN o.product_rating = 1 THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS one_star_percentage
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY m.merchant_name
ORDER BY one_star_percentage DESC;
SELECT m.merchant_name,
ROUND(VAR_SAMP(o.product_rating), 2) AS rating_variance
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY m.merchant_name
ORDER BY rating_variance DESC;
SELECT p.product_name,
ROUND(SUM(CASE WHEN o.product_rating = 5 THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS five_star_percentage
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY five_star_percentage DESC;
SELECT p.product_name,
ROUND(VAR_SAMP(o.product_rating), 2) AS rating_variance
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY rating_variance;
SELECT m.merchant_name,
COUNT(DISTINCT p.product_id) AS highly_rated_products
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.product_rating >= 4
GROUP BY m.merchant_name
ORDER BY highly_rated_products DESC;
SELECT p.product_name,
ROUND(SUM(CASE WHEN o.product_rating < 3 THEN 1 ELSE 0 END) /
COUNT(o.product_rating) * 100, 2) AS low_rating_percentage
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY low_rating_percentage DESC;
SELECT m.merchant_name,
COUNT(DISTINCT p.product_id) AS five_star_products
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.product_rating = 5
GROUP BY m.merchant_name
ORDER BY five_star_products DESC;
SELECT p.category,
DATE_FORMAT(o.order_date, '%Y-%m') AS rating_month,
ROUND(AVG(o.product_rating), 2) AS avg_monthly_rating
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.category, rating_month
ORDER BY p.category, rating_month;
SELECT p.product_name, COUNT(o.order_id) AS one_star_count
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating = 1
GROUP BY p.product_name
ORDER BY one_star_count DESC;
SELECT m.merchant_name,
ROUND(SUM(CASE WHEN o.product_rating < 3 THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS low_rating_percentage
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY m.merchant_name
ORDER BY low_rating_percentage DESC;
SELECT p.product_name,
ROUND(AVG(CASE WHEN o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1
YEAR) THEN o.product_rating END), 2) AS last_year_rating,
ROUND(AVG(o.product_rating), 2) AS overall_rating,
ROUND(AVG(CASE WHEN o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1
YEAR) THEN o.product_rating END) - AVG(o.product_rating), 2) AS rating_growth
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY rating_growth DESC;
SELECT m.merchant_name,
ROUND(STDDEV(o.product_rating), 2) AS rating_std_dev
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY m.merchant_name
ORDER BY rating_std_dev;
SELECT m.merchant_name,
ROUND(AVG(o.product_rating), 2) AS avg_product_rating,
COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) AS total_refunds
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY m.merchant_name
ORDER BY total_refunds DESC;
SELECT p.product_name,
ROUND(AVG(o.product_rating), 2) AS avg_rating,
COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) AS refund_count
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating >= 4
GROUP BY p.product_name
ORDER BY refund_count DESC;
SELECT p.product_name,
ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2) AS
avg_delivery_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_delivery_time;
SELECT m.merchant_name,
ROUND(AVG(DATEDIFF(o.delivery_date, o.shipping_date)), 2) AS
avg_shipping_time
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.shipping_date IS NOT NULL AND o.delivery_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_shipping_time;
SELECT ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2) AS
avg_delivery_time,
ROUND(AVG(o.product_rating), 2) AS avg_rating
FROM orders o
WHERE o.delivery_date IS NOT NULL AND o.product_rating IS NOT NULL
GROUP BY avg_delivery_time
ORDER BY avg_delivery_time;
SELECT p.category,
COUNT(CASE WHEN o.delivery_date > o.estimated_delivery_date THEN 1 END)
AS delayed_deliveries,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.delivery_date > o.estimated_delivery_date THEN
1 END) / COUNT(o.order_id) * 100, 2) AS delay_percentage
FROM products p
JOIN orders o USING(product_id)
WHERE o.estimated_delivery_date IS NOT NULL AND o.delivery_date IS NOT NULL
GROUP BY p.category
ORDER BY delay_percentage DESC;
SELECT m.merchant_name,
COUNT(CASE WHEN o.delivery_date <= o.estimated_delivery_date THEN 1 END)
AS on_time_deliveries
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.estimated_delivery_date IS NOT NULL AND o.delivery_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY on_time_deliveries DESC;
SELECT m.merchant_name,
COUNT(CASE WHEN o.shipping_status = 'Delivered' AND o.refund_requested =
'No' THEN 1 END) AS successful_deliveries,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.shipping_status = 'Delivered' AND
o.refund_requested = 'No' THEN 1 END) / COUNT(o.order_id) * 100, 2) AS
success_rate
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.shipping_status IS NOT NULL
GROUP BY m.merchant_name
ORDER BY success_rate DESC;
SELECT m.merchant_name,
ROUND(AVG(CASE WHEN o.shipping_date >= DATE_SUB(CURDATE(), INTERVAL
1 YEAR) THEN DATEDIFF(o.delivery_date, o.shipping_date) END), 2) AS
last_year_shipping_time,
ROUND(AVG(DATEDIFF(o.delivery_date, o.shipping_date)), 2) AS
overall_shipping_time,
ROUND(AVG(DATEDIFF(o.delivery_date, o.shipping_date)) - AVG(CASE WHEN
o.shipping_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR) THEN
DATEDIFF(o.delivery_date, o.shipping_date) END), 2) AS
shipping_time_improvement
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.shipping_date IS NOT NULL AND o.delivery_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY shipping_time_improvement DESC;
SELECT m.merchant_name,
COUNT(CASE WHEN o.refund_reason = 'Damaged Product' THEN 1 END) AS
damaged_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.refund_reason = 'Damaged Product' THEN 1
END) / COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.refund_reason IS NOT NULL
GROUP BY m.merchant_name
ORDER BY refund_percentage DESC;
SELECT p.product_name,
ROUND(AVG(DATEDIFF(o.shipping_date, o.order_date)), 2) AS
avg_shipping_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipping_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_shipping_time;
SELECT m.merchant_name,
COUNT(CASE WHEN o.refund_requested = 'Yes' AND c.total_orders > 1 THEN 1
END) AS repeat_customer_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.refund_requested = 'Yes' AND c.total_orders > 1
THEN 1 END) / COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
JOIN customers c USING(customer_id)
WHERE o.refund_requested IS NOT NULL
GROUP BY m.merchant_name
ORDER BY refund_percentage DESC;
SELECT p.category,
COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) AS total_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) /
COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM products p
JOIN orders o USING(product_id)
WHERE o.refund_requested IS NOT NULL
GROUP BY p.category
ORDER BY refund_percentage DESC;
SELECT m.merchant_name,
COUNT(CASE WHEN o.shipping_status = 'Undelivered' THEN 1 END) AS
undelivered_orders,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.shipping_status = 'Undelivered' THEN 1 END) /
COUNT(o.order_id) * 100, 2) AS undelivered_percentage
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.shipping_status IS NOT NULL
GROUP BY m.merchant_name
ORDER BY undelivered_percentage DESC;
SELECT
CASE
WHEN o.total_amount < 50 THEN 'Low (Under $50)'
WHEN o.total_amount BETWEEN 50 AND 200 THEN 'Medium ($50-$200)'
ELSE 'High (Over $200)'
END AS order_value_bracket,
COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) AS refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) /
COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM orders o
WHERE o.refund_requested IS NOT NULL
GROUP BY order_value_bracket
ORDER BY refund_percentage DESC;
SELECT p.category,
ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2) AS
avg_delivery_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY p.category
ORDER BY avg_delivery_time DESC;
SELECT
ROUND(
COUNT(DISTINCT CASE
WHEN c2.order_date <= DATE_ADD(c1.order_date, INTERVAL 30 DAY)
THEN c1.customer_id
END)
/ COUNT(DISTINCT c1.customer_id) * 100, 2
) AS repeat_purchase_rate
FROM orders c1
JOIN orders c2 ON c1.customer_id = c2.customer_id AND c1.order_id <>
c2.order_id;
SELECT
CASE
WHEN DATEDIFF(o.order_date, c.last_order_date) <= 30 THEN 'Within 30 days'
WHEN DATEDIFF(o.order_date, c.last_order_date) BETWEEN 31 AND 90 THEN
'31-90 days'
ELSE 'Over 90 days'
END AS time_since_last_purchase,
COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) AS refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) /
COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM orders o
JOIN customers c USING(customer_id)
WHERE o.refund_requested IS NOT NULL
GROUP BY time_since_last_purchase
ORDER BY refund_percentage DESC;
SELECT
CASE
WHEN first_purchase_amount < 50 THEN 'Low (Under $50)'
WHEN first_purchase_amount BETWEEN 50 AND 200 THEN 'Medium ($50-
$200)'
ELSE 'High (Over $200)'
END AS first_purchase_segment,
ROUND(AVG(lifetime_value), 2) AS avg_lifetime_value
FROM customers
GROUP BY first_purchase_segment
ORDER BY avg_lifetime_value DESC;
SELECT m.merchant_name,
COUNT(o.order_id) AS total_orders,
COUNT(DISTINCT CASE WHEN c.lifetime_value > 500 THEN o.customer_id END)
AS high_value_customers
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
JOIN customers c USING(customer_id)
WHERE c.lifetime_value IS NOT NULL
GROUP BY m.merchant_name
ORDER BY high_value_customers DESC;
SELECT
COUNT(CASE WHEN c.total_orders = 1 THEN 1 END) AS first_time_orders,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN c.total_orders = 1 THEN 1 END) / COUNT(o.order_id)
* 100, 2) AS first_time_order_percentage
FROM orders o
JOIN customers c USING(customer_id);
SELECT m.merchant_name,
SUM(o.total_amount) AS total_revenue
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name
ORDER BY total_revenue DESC;
SELECT m.merchant_name,
COUNT(DISTINCT p.product_id) AS unique_products
FROM merchants m
JOIN products p USING(merchant_id)
GROUP BY m.merchant_name
ORDER BY m.merchant_name;
SELECT p.category,
ROUND(AVG(o.total_amount), 2) AS avg_order_value
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.category
ORDER BY avg_order_value DESC;
SELECT c.customer_name,
COUNT(o.order_id) AS total_orders
FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_name
ORDER BY total_orders DESC;
SELECT m.merchant_name,
ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2) AS
avg_delivery_time
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_time;
SELECT p.category,
ROUND(AVG(p.rating), 2) AS avg_rating
FROM products p
WHERE p.rating IS NOT NULL
GROUP BY p.category
ORDER BY avg_rating DESC;
SELECT c.customer_name,
SUM(o.total_amount) AS total_spent
FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_name
ORDER BY total_spent DESC;
SELECT m.merchant_name,
COUNT(o.refund_requested) AS total_refunds
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.refund_requested = 'Yes'
GROUP BY m.merchant_name
ORDER BY total_refunds DESC;
SELECT p.product_name,
ROUND(AVG(DATEDIFF(o.shipping_date, o.order_date)), 2) AS
avg_shipping_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipping_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_shipping_time;
SELECT c.customer_name,
COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) AS
refunded_orders
FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_name
ORDER BY refunded_orders DESC;
SELECT o.shipping_status,
COUNT(o.order_id) AS total_orders,
ROUND(AVG(o.total_amount), 2) AS avg_order_value
FROM orders o
GROUP BY o.shipping_status
ORDER BY total_orders DESC;
SELECT m.merchant_name,
ROUND(AVG(p.rating), 2) AS avg_product_rating
FROM merchants m
JOIN products p USING(merchant_id)
WHERE p.rating IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_product_rating DESC;
SELECT p.category,
COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) AS total_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.refund_requested = 'Yes' THEN 1 END) /
COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.category
ORDER BY refund_percentage DESC;
SELECT m.merchant_name,
COUNT(o.order_id) AS total_orders,
SUM(o.total_amount) AS total_revenue
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name
ORDER BY total_revenue DESC;
SELECT p.category,
ROUND(AVG(DATEDIFF(o.shipping_date, o.order_date)), 2) AS
avg_shipping_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipping_date IS NOT NULL
GROUP BY p.category
ORDER BY avg_shipping_time;
WITH CustomerOrderCount AS (
SELECT customer_id, COUNT(order_id) AS order_count
FROM orders
GROUP BY customer_id
)
SELECT c.customer_name, coc.order_count
FROM customers c
JOIN CustomerOrderCount coc USING(customer_id)
WHERE coc.order_count > (SELECT AVG(order_count) FROM CustomerOrderCount);
WITH MerchantRevenue AS (
SELECT merchant_id, SUM(o.total_amount) AS total_revenue
FROM products p
JOIN orders o USING(product_id)
GROUP BY merchant_id
)
SELECT p.product_name, p.merchant_id, SUM(o.total_amount) AS product_revenue
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name, p.merchant_id
HAVING SUM(o.total_amount) > 0.1 * (SELECT total_revenue
FROM MerchantRevenue
WHERE merchant_id = p.merchant_id);
SELECT c.customer_name, SUM(o.total_amount) AS total_spent,
RANK() OVER (ORDER BY SUM(o.total_amount) DESC) AS spending_rank
FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_name;
WITH MerchantRevenue AS (
SELECT m.merchant_name, SUM(o.total_amount) AS total_revenue
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name
)
SELECT merchant_name, total_revenue,
ROUND(100.0 * total_revenue / (SELECT SUM(total_revenue) FROM
MerchantRevenue), 2) AS revenue_percentage
FROM MerchantRevenue;
SELECT customer_id,
MIN(order_date) AS first_order,
MAX(order_date) AS last_order
FROM orders
GROUP BY customer_id;
WITH OrderDifferences AS (
SELECT customer_id, order_date,
LEAD(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)
AS next_order_date
FROM orders
)
SELECT customer_id, AVG(DATEDIFF(next_order_date, order_date)) AS
avg_days_between_orders
FROM OrderDifferences
WHERE next_order_date IS NOT NULL
GROUP BY customer_id;
WITH MerchantAverages AS (
SELECT merchant_id, AVG(price) AS avg_price, AVG(rating) AS avg_rating
FROM products
GROUP BY merchant_id
)
SELECT p.product_name, p.price, p.rating
FROM products p
JOIN MerchantAverages ma USING(merchant_id)
WHERE p.price > ma.avg_price AND p.rating > ma.avg_rating;
WITH RecentOrders AS (
SELECT customer_id, total_amount, order_date
FROM orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
)
SELECT c.customer_name, SUM(ro.total_amount) AS total_spent
FROM customers c
JOIN RecentOrders ro USING(customer_id)
GROUP BY c.customer_name
ORDER BY total_spent DESC
LIMIT 3;
WITH MerchantOrderAvg AS (
SELECT m.merchant_name, AVG(o.total_amount) AS avg_order_value
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name
)
SELECT merchant_name, avg_order_value
FROM MerchantOrderAvg
WHERE avg_order_value > (SELECT AVG(avg_order_value) FROM
MerchantOrderAvg);
WITH CategoryAvgRating AS (
SELECT category, AVG(rating) AS avg_rating
FROM products
GROUP BY category
)
SELECT p.product_name, p.category, p.rating
FROM products p
JOIN CategoryAvgRating car USING(category)
WHERE p.rating > car.avg_rating;
WITH MonthlyRevenue AS (
SELECT merchant_id, DATE_FORMAT(order_date, '%Y-%m') AS month,
SUM(total_amount) AS revenue
FROM orders o
JOIN products p USING(product_id)
GROUP BY merchant_id, month
)
SELECT m.merchant_name
FROM MonthlyRevenue mr
JOIN merchants m USING(merchant_id)
WHERE revenue > LAG(revenue, 1) OVER (PARTITION BY merchant_id ORDER BY
month)
AND LAG(revenue, 1) OVER (PARTITION BY merchant_id ORDER BY month) >
LAG(revenue, 2) OVER (PARTITION BY merchant_id ORDER BY month);
WITH CustomerOrderCount AS (
SELECT customer_id, COUNT(order_id) AS total_orders
FROM orders
GROUP BY customer_id
)
SELECT c.customer_name, coc.total_orders
FROM customers c
JOIN CustomerOrderCount coc USING(customer_id)
ORDER BY total_orders DESC
LIMIT 10;
WITH MerchantRefunds AS (
SELECT p.merchant_id, COUNT(o.order_id) AS total_orders,
SUM(CASE WHEN o.refund_requested = 'Yes' THEN 1 ELSE 0 END) AS
refunded_orders
FROM orders o
JOIN products p USING(product_id)
GROUP BY p.merchant_id
)
SELECT m.merchant_name,
(100.0 * mr.refunded_orders / mr.total_orders) AS refund_percentage
FROM MerchantRefunds mr
JOIN merchants m USING(merchant_id)
WHERE mr.total_orders > 0;
WITH CategorySales AS (
SELECT p.category, SUM(o.total_amount) AS total_sales
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.category
)
SELECT category, total_sales
FROM CategorySales
ORDER BY total_sales DESC
LIMIT 1;
WITH CustomerOrders AS (
SELECT customer_id, order_id, total_amount,
RANK() OVER (PARTITION BY customer_id ORDER BY order_date) AS
first_order_rank,
RANK() OVER (PARTITION BY customer_id ORDER BY total_amount DESC) AS
largest_order_rank
FROM orders
)
SELECT c.customer_name, co.order_id, co.total_amount
FROM CustomerOrders co
JOIN customers c USING(customer_id)
WHERE first_order_rank = 1 AND largest_order_rank = 1;
WITH CustomerOrderValues AS (
SELECT customer_id, order_date, total_amount,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS
order_num
FROM orders
)
SELECT customer_id, order_date, total_amount,
AVG(total_amount) OVER (PARTITION BY customer_id ORDER BY order_num
ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) AS moving_avg
FROM CustomerOrderValues;
WITH ProductRankings AS (
SELECT merchant_id, product_name, rating,
RANK() OVER (PARTITION BY merchant_id ORDER BY rating DESC) AS rank
FROM products
WHERE rating IS NOT NULL
)
SELECT m.merchant_name, pr.product_name, pr.rating
FROM ProductRankings pr
JOIN merchants m USING(merchant_id)
WHERE rank = 1;
WITH CustomerOrderTrend AS (
SELECT customer_id, order_date, total_amount,
LAG(total_amount, 1) OVER (PARTITION BY customer_id ORDER BY
order_date) AS prev_order_value,
LAG(total_amount, 2) OVER (PARTITION BY customer_id ORDER BY
order_date) AS second_prev_order_value
FROM orders
)
SELECT c.customer_name, cot.order_date, cot.total_amount
FROM CustomerOrderTrend cot
JOIN customers c USING(customer_id)
WHERE total_amount < prev_order_value AND prev_order_value <
second_prev_order_value;
WITH MerchantTotalSales AS (
SELECT merchant_id, SUM(o.total_amount) AS merchant_sales
FROM products p
JOIN orders o USING(product_id)
GROUP BY merchant_id
)
SELECT p.product_name, m.merchant_name,
SUM(o.total_amount) AS product_sales,
ROUND(100.0 * SUM(o.total_amount) / mts.merchant_sales, 2) AS
sales_percentage
FROM products p
JOIN orders o USING(product_id)
JOIN MerchantTotalSales mts USING(merchant_id)
JOIN merchants m USING(merchant_id)
GROUP BY p.product_name, m.merchant_name, mts.merchant_sales;
WITH RecentSales AS (
SELECT DISTINCT product_id
FROM orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
)
SELECT p.product_name
FROM products p
LEFT JOIN RecentSales rs USING(product_id)
WHERE rs.product_id IS NULL;
WITH MerchantTransactions AS (
SELECT p.merchant_id, MIN(o.order_date) AS first_transaction,
MAX(o.order_date) AS last_transaction
FROM orders o
JOIN products p USING(product_id)
GROUP BY p.merchant_id
)
SELECT m.merchant_name, mt.first_transaction, mt.last_transaction
FROM MerchantTransactions mt
JOIN merchants m USING(merchant_id);