0% found this document useful (0 votes)
2 views

Backup

The document outlines a comprehensive set of queries related to product sales, customer orders, and merchant performance. It includes requests for data on product names and prices, customer details, order statistics, revenue calculations, and refund rates. The queries aim to analyze customer behavior, product demand, and merchant efficiency to enhance business insights and decision-making.

Uploaded by

hanna7viikila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Backup

The document outlines a comprehensive set of queries related to product sales, customer orders, and merchant performance. It includes requests for data on product names and prices, customer details, order statistics, revenue calculations, and refund rates. The queries aim to analyze customer behavior, product demand, and merchant efficiency to enhance business insights and decision-making.

Uploaded by

hanna7viikila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 268

Question

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

Show the customers who haven’t placed any orders.

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

Get the total revenue per product category

List all products that have never been ordered.

Fetch the top 3 customers who have spent the most money.

Get the average order value for each customer.

Show the products along with the total quantity ordered and total revenue generated for each product.

Get the monthly revenue from the 'orders' table

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.

Get the average order value for each month.

Fetch the top 5 highest-value transactions with customer details.

Identify products with total sales revenue greater than $10000.

Find the customers who placed the most transactions.


Get the total revenue and number of transactions for each product category.

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.

Identify orders that include more than 3 different products.

Find customers who haven’t ordered anything in the past 12 months.

Get the most frequently ordered product along with the total quantity ordered.

Calculate the revenue contribution percentage of each product category.

Find the average time (in days) between a customer’s orders.


Identify the day of the week with the highest average order value.

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.

Identify customers who haven’t placed an order in the last 6 months


Get the average order frequency (in days) per customer and identify those whose frequency has dropped below average.

Segment customers into high, medium, and low spenders based on their total spending.

Group customers by order frequency and total spending


Find slow-moving products — those with low total sales over the past year.

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).

Identify products frequently going out of stock.

Calculate average lead time between product orders and delivery date.
Predict top-selling products for the next quarter based on past quarterly sales trends.

Analyze product return rates and identify high-return products.

Identify suppliers with the fastest average delivery time.

Track profitability trends by month over the past year.


Calculate customer lifetime value (CLV) based on average order value, purchase frequency, and customer lifespan.

Segment customers based on RFM (Recency, Frequency, Monetary) analysis.


Predict next month’s product demand based on monthly 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.

Identify seasonal product demand based on historical sales.

Calculate average customer retention rate over the past 12 months.


Identify high-potential customers — those with increasing purchase frequency and spend.

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.

Analyze the profit margin for each product category.


Predict next quarter’s product demand based on sales trends.
Calculate customer retention rate and churn rate for the past 12 months.

Find the median, standard deviation, and 90th percentile of order values.
Identify top and bottom 5 products by revenue and their average order value.

Identify high-spending outlier customers based on order value distribution.


Calculate inventory turnover rate by product over the last year.

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.

Estimate CLV with an adjusted average order value considering discounts.


Predict product demand based on moving average of sales over the last 3 months.

Identify top and bottom 5 products by revenue and their average order value.
Calculate the average number of days between orders for each customer.

Measure average order fulfillment time and identify slowest suppliers.

Calculate the lifespan of each customer based on their first and most recent order.
Calculate inventory turnover rate by product over the last year.

Identify customers who haven't placed an order in the last 6 months.

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 contribution margin and gross profit percentage by product category.

Measure the percentage of orders delivered late.


Segment customers based on their average days between purchases.

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.

Identify customers whose total spending is in the top 5 percent.


Measure how often suppliers deliver on time.

Identify customers who haven't placed an order in the last year.


Determine the top-selling product each month based on revenue.

Can I see a list of all my past orders with the total amount and order date?

Which products have I bought the most?


How much have I spent on orders in the last year?

What’s the current delivery status of my recent orders?

When was my last purchase and what did I buy?

Which product category do I purchase from the most?


What’s my average spend per order?

How often do I place an order?

What similar products do other customers who bought the same items purchase?

How has my spending changed month by month?


How often do customers return to make additional purchases

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 average product ratings?

Which merchant have I purchased from the most?


Which merchants deliver orders on time most often?

How much have I spent with each merchant?

What are my most recent purchases from a specific merchant?


Which merchant do I buy from the most often?

Which merchant delivers my orders the fastest on average?


How much have I spent with each merchant over the past six months?

Which merchant do most customers buy from?

Which merchant offers the widest variety of products?

Which merchant offers the best discounts on their products?


How often do I place orders from each merchant?

Which merchant has earned the most revenue from my purchases?

Which merchant did I last buy from?


Which merchants consistently deliver my orders on time?

Which merchants offer the highest-rated products?

Which merchants have the highest number of refunded orders?


How often does each merchant deliver my orders on time or late?

Which merchants have the highest return rates for their products?

Which merchants process refunds the fastest?


How has customer satisfaction changed over time for each merchant?

How has the refund rate changed over the past year for each merchant?

Which merchants have high ratings and low refund rates?


Which merchants retain customers over time?

Is there a correlation between delivery speed and customer satisfaction for each merchant?

Which merchants generate the highest profits?


Which merchants have the fastest-moving inventory?

Which customers contribute the most profit to each merchant?

Which merchants deliver products fastest on average?


How can we segment customers based on spending and order frequency for each merchant?

Which product categories drive the most revenue for each merchant?

How has each merchant’s profitability evolved month by month?


Which customers generate the highest lifetime value 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?

How can we forecast demand for the most popular products?

Which products generate the most profit for each merchant?


Which merchants have the highest customer churn rates?

Which products are at risk of running out of stock?

Which customers are likely to generate the most value over time?
Which customers generate the highest lifetime profit?

Which products have low sales and high stock?

How can we classify customers based on their purchase behavior?

Which products drive the most profit for each merchant?


Which customers haven’t purchased in a long time and may be at risk of churning?

Which products show consistent demand trends over time?

Which customer segments drive the most profit, and how frequently do they purchase?

Which products show consistent demand growth month over month?


Which merchants generate the highest profit relative to their number of orders?

Which products have the fastest and slowest inventory turnover?

Which customers are likely to deliver the highest future value?


How do refund rates affect merchant profitability?

Which merchants have the highest customer retention rates?

How has profitability evolved month over month


Which customers place frequent high-value orders?

Which products are likely to need replenishment soon?

Which merchants manage refunds efficiently without impacting profitability?


Which customers are most likely to churn based on their inactivity?

How do refund rates for each merchant change over time

Which customers have the highest refund rates, indicating dissatisfaction


Which products have high refund rates and low profitability

Which merchants manage their inventory turnover most efficiently

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

How has customer dissatisfaction evolved over the last year


Which customers frequently request refunds, signaling dissatisfaction

How much do refunds reduce overall profitability per merchant

Which customers consistently refund orders over an extended period


Which customers contribute the most revenue to each merchant

Which customers have the highest refund impact on each merchant’s revenue

Which customers drive long-term profitability for each merchant


Which customers place the most frequent orders with each merchant

Which high-spending customers also have high refund rates per merchant

Which merchants have the most satisfied (lowest refunding) customers


Which products are seeing rising demand over the past 6 months

Which products contribute the most and least to each merchant's profitability

How efficient are merchants in fulfilling orders


How much do refunds reduce profitability for each merchant

Which merchants manage inventory turnover most efficiently


Which high-value customers also refund the most, hurting merchant 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 merchants experience the most delays in product deliveries


Which products sell through their stock the fastest

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

Which merchants deliver their products fastest on average


How often do late deliveries lead to refund requests

Which merchants and products contribute most to platform-wide profits

Which merchants manage their inventory most efficiently


Which customer segments show the highest and lowest satisfaction levels

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 frequently do repeat customers place new orders


Which merchants ship their products the fastest after an order is placed

Which customers contribute the highest and lowest revenue

Which product categories experience the highest refund rates

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

How many orders has each customer placed

What’s the average amount each customer spends per order

Which products are sold the most


What’s the refund rate for each merchant

Which products generate the highest total profit?

How often does each customer place an order

Which products get refunded the most

How has revenue changed month over month

Which customers request the most refunds


Which merchants bring in the most revenue

Which orders are currently in transit?

How long does each merchant take to ship orders on average

Which merchants have the most late deliveries

What percentage of orders are delivered on time?

Which products take the longest to ship on average?


Which orders were delivered before the expected date?

How many orders are in each shipping status?

How long does it take to deliver each product on average?

Which customers experience the most delayed deliveries

Which merchants consistently deliver their products the fastest?

Which orders haven’t been shipped yet

How long does it usually take for an order to be delivered?


Which products are shipped the fastest on average?

Which merchants have the most orders currently in transit?

Which merchants have the highest on-time delivery rates?

Which customers are still waiting for their orders to arrive?

Which products are most often delivered late


How long do merchants' orders typically stay in transit?

Which orders arrived exactly on their expected delivery date?

Which merchants take the longest time to ship their orders?

How much has each customer spent in total

What’s the average transaction amount for each merchant?

How many transactions happen each day?

Which are the highest-value transactions?


How often does each customer make a transaction?

How many transactions does each merchant process?

How much revenue does each merchant make per day

Which customers make the most transactions?

How has each merchant’s revenue changed over time

What’s the biggest transaction for each merchant?


How many transactions does each customer make per month

How much revenue is generated on each transaction date?

Which customers spend the most on average per transaction?

How many transactions happen on weekends?

Which merchants generate the most revenue?

What’s the average transaction value per day?

Which customers have the longest time between transactions?


Which merchants have the highest transaction frequency?

When did each customer make their first and most recent transaction?

How many transactions does each merchant process daily?

Monthly Revenue Per Merchant

Total Transactions Per Customer Per Month

Average Transaction Value Per Day

Highest Spending Customers


Merchants With the Most Transactions

Daily Transaction Count

Monthly Average Transaction Amount

Total Revenue by Merchant

Transactions on Holidays or Weekends

Average Number of Transactions Per Customer

Monthly Transaction Growth Rate


Merchants With Fastest Revenue Growth

Customers With Consistent Monthly Spending

Transactions With the Longest Delay Between Order and Shipment

Average Shipping Time Per Merchant

Refund Rate Per Merchant


Total Refund Amount Per Customer

In-Transit Orders Per Merchant

Late Delivery Rate Per Merchant

Customer Lifetime Spend

Monthly Refund Rate

Top 10 Products by Revenue


Average Delivery Time Per Merchant

High-Value Customers

Monthly Shipping Delays

Merchant Performance Comparison by Delivery Speed

Customer Churn Analysis

Most Frequently Returned Products


Inventory Turnover Rate Per Product

Daily Revenue Trend

Top five Merchants by Refund Rate

(Average In-Transit Time Per Merchant

Monthly Customer Churn Rate


Products With the Fastest Inventory Turnover

Average Customer Spend Per Order

Delayed Orders Count Per Merchant

Customer Refund Frequency

Shipping Time Variability Per Merchant

Customer Order Frequency

Total Transactions Per Customer


Average Transaction Amount Per Customer

Most Popular Product by Orders

Merchant Revenue Ranking

Orders Delivered On Time

Customer Satisfaction Based on Refunds

Average Shipping Time by Merchant


Product Performance by Revenue

Monthly Refund Trends

Merchant Delivery Efficiency

Which customer has made the highest number of transactions?

What is the average transaction amount for each customer?

Which product has been ordered the most times?


Which merchant has generated the highest revenue?

How many orders were delivered on time within five days?

Which customers have the highest refund percentage?

What is the average shipping time for each merchant?

Which products have generated the highest revenue?

How many refunds happened each month?


How many orders per merchant were delivered, delayed, or still in transit?

What is the total revenue generated per month?

Which customer placed the highest number of refund requests?

What is the average delivery time for each product?

Which merchant has the fastest average shipping time?


How many customers have placed more than ten orders?

What is the total number of delayed shipments per merchant?

What is the average order value for each customer?

Which product has the highest return rate?

How many orders were shipped on the same day they were placed?

Which customer has the highest total spending?


Which product has the highest average rating from customers?

How many days on average does each merchant take to deliver orders?

Which customers have spent the most overall?

What is the percentage of delayed orders per merchant?

What is the monthly trend of in-transit orders?

Which product categories generate the most revenue?


Which merchants have the highest refund rates?

How many customers have placed repeat orders?

What’s the average transaction amount per product?

Which merchants deliver the fastest on average?

Which product has the highest number of refunds?

What’s the average time between order and delivery for each product category?
Which customers have the highest order frequency?

What’s the percentage of on-time deliveries for each merchant?

Which products have the longest average shipping times?

How many orders were placed in each delivery status category?

What’s the total revenue generated by each merchant?

Which customers have the highest refund rate?


What is the total number of in-transit orders per month?

Which product categories have the fastest average delivery times?

Which merchants sold the most products, and how much revenue did they generate?

What’s the average product rating for each merchant?

How many unique customers have purchased from each merchant?


Which products have the highest refund rates, and which merchants sell them?

What’s the average delivery time for each merchant’s products?

Which customers have the highest total spend, and which merchants do they buy from?

Which products have the fastest shipping times by merchant?

What’s the monthly revenue trend for each merchant?


Which customers have the highest refund rates with specific merchants?

Which merchants handle the most in-transit orders?

Which customers have the fastest average delivery times across all their orders?

Which merchants experience the highest percentage of delayed deliveries?

What’s the average transaction amount for each product category?


Which merchants see the most frequent refunds?

Which customers have made the most orders but have the lowest total spending?

What’s the refund rate for each product category?

Which products have the highest customer satisfaction ratings?

Which customers consistently rate products the highest?


Which merchants have the fastest average shipping times?

Which product categories have the highest revenue contribution?

Which customers have the highest average order value?

Which merchants have the most in-transit orders currently?

What’s the percentage of refunded orders for each product?


Which customers have the longest average delivery times?

Which merchants generate the highest monthly revenue?

Which customers have the highest refund rates?

Which products have the fastest shipping times?

What’s the total revenue and average order value for each merchant?
Which customers place the most in-transit orders?

Which product categories experience the highest refund rates?

Which merchants have the fastest average time from order to shipping?

Which customers have the highest total transaction amount over the last six months?

What’s the average product rating per merchant?


Which products have the highest refund count?

Which customers have the highest number of late deliveries?

What’s the total revenue and refund amount per merchant?

Which product categories have the fastest average delivery times?

Which customers have the most repeat purchases of a single product?


What’s the monthly refund rate for each merchant?

Which customers have the highest dissatisfaction

Which merchants face the highest rate of product returns?

Which products have the highest difference between order date and shipping date?

Which customers have the highest average delay in delivery time?


What’s the total number of orders, refunds, and in-transit shipments for each merchant?

Which products have the lowest customer ratings and highest refund rates?

Which merchants have the fastest and slowest average order fulfillment time?

What’s the monthly trend of late deliveries for each merchant?


Which customers have the highest number of different products purchased?

Which product categories generate the most revenue?

Which customers have the highest percentage of refunded orders?

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 highest number of orders?

What’s the average customer rating for each product category?

Which products have the highest refund rates?


What’s the total revenue generated by each product?

Which products have the fastest shipping times?

What’s the average delivery delay for each product category?

Which products have the highest number of late deliveries?

Which products have the lowest customer satisfaction ratings?

Which product categories have the highest number of in-transit orders?


Which products contribute most to total revenue?

Which products have the highest number of unique customers?

Which products have the fastest average time from order to delivery?

Which product categories have the highest refund percentages?

Which products have the longest time between order and shipping?

Which products generate the highest average transaction value?


Which products are most often involved in refunded orders?

Which product categories have the highest delivery delays?

Which products have the most consistent on-time delivery?

Which product categories attract the most unique customers?

Which products have the highest average order value?

What’s the percentage of refunded orders for each product?


Which products have the fastest time from shipping to delivery?

Which product categories generate the most revenue?


Which products experience the longest time between order and refund?

What’s the average transaction amount per product category?

Which products have the highest late delivery rates?

Which product categories have the fastest average shipping times?

Which products see the highest frequency of customer complaints?


What’s the total quantity of each product sold?

Which products have the fastest refund processing times?

Which product categories have the lowest return rates?

Which products have the highest number of on-time deliveries?

Which products have the highest transaction volume?

Which products have the highest number of orders still in transit?


Which product categories show the highest customer satisfaction rates?

Which products contribute most to late delivery complaints?

Which product categories have the most efficient shipping processes?

Which products see the highest rate of early deliveries?

Which products have the lowest average order value?

What is the average product rating for each product?


Which merchants have the highest average product ratings across their sales?

Which products receive the most five star ratings?

What is the distribution of product ratings for each merchant?

Which merchants have the highest percentage of one star ratings?

Which products have the most customer reviews (ratings)?


What is the average product rating for each product category?

Which merchants have the widest spread of product ratings (highest rating variance)?

Which products have the highest percentage of perfect five star ratings?

Which merchants have the lowest average product ratings?

Which products have the highest consistency in ratings (lowest rating variance)?
Which merchants have the most highly-rated products on average?

What percentage of each product’s ratings are below three stars?

Which merchants have the highest number of products with perfect five star ratings?

What is the average product rating over time (by month)?

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 have the fastest average shipping times?


How does delivery time impact product ratings?

Which product categories experience the highest rate of delayed deliveries?

Which merchants handle the highest volume of on-time deliveries?

How often do customers request refunds for late deliveries?


Which products experience the highest rate of in-transit delays?

Which merchants maintain the highest percentage of successful deliveries without issues?

How does shipping time affect customer satisfaction ratings?


Which products are most likely to face shipping issues (e.g., long transit times)?

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?

What is the relationship between product category and refund rates?


Which merchants have the highest percentage of undelivered orders?

How does refund rate change by order value bracket?

Which product categories have the longest average delivery time?


What percentage of customers make a second purchase within 30 days of their first order?

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 percentage of orders come from first-time 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?

How many orders are placed each month, ordered chronologically?

Which product categories have the highest average product rating?

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?

Which customers have the highest number of refunded orders?

What is the total number of orders and average order value for each shipping status?

Which merchants see the highest average product rating?

What is the percentage of refunded orders for each product category?


How many orders and total revenue has each merchant made, sorted by revenue?

Which product categories have the fastest shipping times on average?

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 the most recent order for each customer.

Find the average order amount per shipping status.

Identify merchants whose highest-priced product is above the average product price across all merchants.

Calculate the percentage contribution of each merchant’s revenue to total revenue.

Get the three most popular products based on order count.


Identify customers who requested the most refunds.

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.

Determine the average days between a customer’s orders.


Identify merchants who have products with both high ratings and high prices compared to their averages.

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.

Identify products with above-average ratings in their respective categories.


Find merchants whose monthly revenue increased consecutively for the last 3 months.

Rank customers by their total number of orders and show only the top 10.

Calculate the percentage of refunded orders for each merchant.


Get the most popular product category by total sales.

Identify customers whose first order was their largest order.

Calculate the moving average of order value per customer over their last 5 orders.

Determine the top-rated product for each merchant.


Identify customers with declining order value over their last 3 purchases.

Calculate each product’s share of its merchant’s total sales.

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 c.email FROM customers c


JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.email
HAVING COUNT(o.order_id) > 5;

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, SUM(o.total_amount) AS total_spent


FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name;

SELECT o.order_id, p.product_name, od.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;

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, SUM(od.quantity) AS total_ordered


FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name
ORDER BY total_ordered DESC
LIMIT 1;
SELECT c.customer_name, 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_name;

SELECT p.category, SUM(od.quantity * p.price) AS total_revenue


FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.category;

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 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_name
ORDER BY total_spent DESC
LIMIT 3;

SELECT c.customer_name, AVG(o.total_amount) AS avg_order_value


FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name;

SELECT p.product_name, SUM(od.quantity) AS total_quantity, SUM(od.quantity *


p.price) AS total_revenue
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name;

SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(total_amount) AS


monthly_revenue
FROM orders
GROUP BY DATE_FORMAT(order_date, '%Y-%m')
ORDER BY month;

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 YEAR(order_date) AS year, COUNT(order_id) AS total_transactions,


SUM(total_amount) AS total_revenue
FROM orders
GROUP BY YEAR(order_date)
ORDER BY year;

SELECT 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_name
HAVING SUM(o.total_amount) > 5000
ORDER BY total_spent DESC;

SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, AVG(total_amount) AS


avg_order_value
FROM orders
GROUP BY DATE_FORMAT(order_date, '%Y-%m')
ORDER BY month;

SELECT o.order_id, o.total_amount, c.customer_name, c.email


FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
ORDER BY o.total_amount DESC
LIMIT 5;

SELECT p.product_name, SUM(od.quantity * p.price) AS total_revenue


FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name
HAVING SUM(od.quantity * p.price) > 10000
ORDER BY total_revenue DESC;

SELECT c.customer_name, COUNT(o.order_id) AS total_orders


FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
ORDER BY total_orders DESC
LIMIT 1;
SELECT p.category, COUNT(od.order_id) AS total_transactions, SUM(od.quantity *
p.price) AS total_revenue
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.category
ORDER BY total_revenue DESC;

SELECT c.customer_name, c.email, SUM(o.total_amount) AS lifetime_value,


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_name, c.email
ORDER BY lifetime_value DESC
LIMIT 5;

SELECT p.product_name, SUM(od.quantity * p.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
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY p.product_name
ORDER BY total_revenue DESC
LIMIT 1;

SELECT c.customer_name, COUNT(o.order_id) AS total_orders,


AVG(o.total_amount) AS avg_order_value
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
HAVING COUNT(o.order_id) > 3
ORDER BY total_orders DESC;

SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(total_amount) AS


monthly_revenue
FROM orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY DATE_FORMAT(order_date, '%Y-%m')
ORDER BY month;

SELECT p.product_name, SUM(od.quantity) AS total_ordered


FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name
HAVING SUM(od.quantity) < 5
ORDER BY total_ordered ASC;
SELECT c.city, SUM(o.total_amount) AS total_revenue
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.city
ORDER BY total_revenue DESC
LIMIT 3;

SELECT od.order_id, COUNT(DISTINCT od.product_id) AS product_count


FROM order_details od
GROUP BY od.order_id
HAVING COUNT(DISTINCT od.product_id) > 3;

SELECT c.customer_name, c.email


FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_date IS NULL OR o.order_date < DATE_SUB(CURDATE(), INTERVAL
12 MONTH);

SELECT p.product_name, SUM(od.quantity) AS total_quantity


FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name
ORDER BY total_quantity DESC
LIMIT 1;

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, AVG(DATEDIFF(o2.order_date, o1.order_date)) AS


avg_days_between_orders
FROM orders o1
JOIN orders o2 ON o1.customer_id = o2.customer_id AND o2.order_date >
o1.order_date
JOIN customers c ON o1.customer_id = c.customer_id
GROUP BY c.customer_name;
SELECT DAYNAME(order_date) AS day_of_week, AVG(total_amount) AS
avg_order_value
FROM orders
GROUP BY DAYNAME(order_date)
ORDER BY avg_order_value DESC
LIMIT 1;

SELECT c.customer_name, p.product_name, COUNT(od.order_id) AS order_count


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, p.product_name
HAVING COUNT(od.order_id) > 1;

SELECT CONCAT(YEAR(order_date), ' Q', QUARTER(order_date)) AS quarter,


COUNT(order_id) AS total_orders,
SUM(total_amount) AS total_revenue
FROM orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 2 YEAR)
GROUP BY YEAR(order_date), QUARTER(order_date)
ORDER BY quarter;

SELECT c.customer_name, MIN(o.order_date) AS first_order_date


FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
HAVING MIN(o.order_date) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);

SELECT c.customer_id, c.customer_name, c.email, MAX(o.order_date) AS


last_order_date
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name, c.email
HAVING MAX(o.order_date) < DATE_SUB(CURDATE(), INTERVAL 6 MONTH) OR
MAX(o.order_date) IS NULL;
WITH order_frequency AS (
SELECT o.customer_id,
AVG(DATEDIFF(o2.order_date, o.order_date)) AS avg_days_between_orders
FROM orders o
JOIN orders o2 ON o.customer_id = o2.customer_id AND o2.order_date >
o.order_date
GROUP BY o.customer_id
)
SELECT c.customer_name, ofq.avg_days_between_orders
FROM customers c
JOIN order_frequency ofq ON c.customer_id = ofq.customer_id
WHERE ofq.avg_days_between_orders > (SELECT AVG(avg_days_between_orders)
FROM order_frequency);

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;

SELECT p.product_name, p.stock_quantity, SUM(od.quantity) AS total_sold


FROM products p
LEFT JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name, p.stock_quantity
HAVING p.stock_quantity > 100 AND SUM(od.quantity) < 20;

SELECT p.product_name, p.stock_quantity, AVG(od.quantity) AS avg_daily_sales,


CASE
WHEN p.stock_quantity < (AVG(od.quantity) * 7) THEN 'Reorder Soon'
ELSE 'Stock OK'
END AS stock_status
FROM products p
JOIN order_details od ON p.product_id = od.product_id
GROUP BY p.product_name, p.stock_quantity
ORDER BY stock_status;

SELECT c.customer_name, COUNT(o.order_id) AS total_orders, 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_name
HAVING COUNT(o.order_id) > 5 AND MAX(o.order_date) < DATE_SUB(CURDATE(),
INTERVAL 3 MONTH);

SELECT c.customer_name, COUNT(o.order_id) AS total_orders,


SUM(o.total_amount) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_name
HAVING COUNT(o.order_id) > 15 AND SUM(o.total_amount) > 10000
ORDER BY total_spent DESC;
SELECT p.product_name, WEEK(o.order_date) AS week_number, 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, WEEK(o.order_date)
ORDER BY p.product_name, week_number;

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, p.stock_quantity, COUNT(o.order_id) AS total_orders


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 p.stock_quantity < 10
GROUP BY p.product_name, p.stock_quantity
ORDER BY total_orders DESC;

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 p.product_name, COUNT(r.return_id) AS total_returns,


COUNT(r.return_id) / COUNT(od.order_id) * 100 AS return_rate_percentage
FROM products p
JOIN order_details od ON p.product_id = od.product_id
LEFT JOIN returns r ON od.order_id = r.order_id
GROUP BY p.product_name
HAVING COUNT(r.return_id) > 0
ORDER BY return_rate_percentage 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;

SELECT DATE_FORMAT(o.order_date, '%Y-%m') AS month,


SUM(od.quantity * (p.price - p.cost)) AS total_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
WHERE o.order_date >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY DATE_FORMAT(o.order_date, '%Y-%m')
ORDER BY month;
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;

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 customer_id, customer_name, COUNT(order_id) AS total_orders


FROM orders
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT customer_id, customer_name, ROUND(AVG(total_amount), 2) AS


avg_order_value
FROM orders
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT product_id, product_name, SUM(quantity) AS total_sold


FROM order_details
JOIN products USING(product_id)
GROUP BY product_id, product_name
ORDER BY total_sold DESC;
SELECT m.merchant_name, COUNT(r.refund_id) / COUNT(o.order_id) * 100 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;

SELECT p.product_name, SUM(od.quantity * (od.unit_price - p.cost_price)) AS


total_profit
FROM products p
JOIN order_details od USING(product_id)
GROUP BY p.product_name
ORDER BY total_profit DESC;

SELECT customer_id, customer_name, ROUND(AVG(DATEDIFF(LEAD(order_date)


OVER (PARTITION BY customer_id ORDER BY order_date), order_date)), 2) AS
avg_days_between_orders
FROM orders
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT p.product_name, COUNT(r.refund_id) / COUNT(od.order_id) * 100 AS


refund_rate
FROM products p
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
LEFT JOIN refunds r USING(order_id)
GROUP BY p.product_name
ORDER BY refund_rate DESC;

SELECT DATE_FORMAT(order_date, '%Y-%m') AS order_month,


ROUND(SUM(total_amount), 2) AS total_revenue
FROM orders
GROUP BY order_month
ORDER BY order_month;

SELECT c.customer_name, COUNT(r.refund_id) AS total_refunds


FROM customers c
JOIN orders o USING(customer_id)
JOIN refunds r USING(order_id)
GROUP BY c.customer_name
ORDER BY total_refunds DESC;
SELECT m.merchant_name, ROUND(SUM(od.quantity * od.unit_price), 2) AS
total_revenue
FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
GROUP BY m.merchant_name
ORDER BY total_revenue DESC
LIMIT 5;

SELECT order_id, customer_name, order_date, shipped_date, delivery_status


FROM orders
JOIN customers USING(customer_id)
WHERE delivery_status = 'In Transit';

SELECT m.merchant_name, ROUND(AVG(DATEDIFF(shipped_date, order_date)), 2)


AS avg_shipping_time
FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE shipped_date IS NOT NULL
GROUP BY m.merchant_name;

SELECT m.merchant_name, COUNT(o.order_id) AS delayed_deliveries


FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE o.delivery_status = 'Delayed'
GROUP BY m.merchant_name
ORDER BY delayed_deliveries DESC;

SELECT ROUND(SUM(CASE WHEN delivery_status = 'Delivered' THEN 1 ELSE 0


END) / COUNT(order_id) * 100, 2) AS on_time_delivery_rate
FROM orders;

SELECT p.product_name, ROUND(AVG(DATEDIFF(shipped_date, order_date)), 2) AS


avg_shipping_time
FROM products p
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE shipped_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_shipping_time DESC;
SELECT order_id, customer_name, shipped_date, delivery_date,
expected_delivery_date
FROM orders
JOIN customers USING(customer_id)
WHERE delivery_date < expected_delivery_date;

SELECT delivery_status, COUNT(order_id) AS total_orders


FROM orders
GROUP BY delivery_status;

SELECT p.product_name, ROUND(AVG(DATEDIFF(delivery_date, order_date)), 2) AS


avg_delivery_time
FROM products p
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE delivery_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_delivery_time;

SELECT c.customer_name, COUNT(o.order_id) AS late_deliveries


FROM customers c
JOIN orders o USING(customer_id)
WHERE o.delivery_status = 'Delayed'
GROUP BY c.customer_name
ORDER BY late_deliveries DESC;

SELECT m.merchant_name, ROUND(AVG(DATEDIFF(delivery_date, order_date)), 2)


AS avg_delivery_time
FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE delivery_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_delivery_time;

SELECT order_id, customer_name, order_date


FROM orders
JOIN customers USING(customer_id)
WHERE shipped_date IS NULL;

SELECT ROUND(AVG(DATEDIFF(delivery_date, order_date)), 2) AS


avg_order_to_delivery_time
FROM orders
WHERE delivery_date IS NOT NULL;
SELECT p.product_name, ROUND(AVG(DATEDIFF(shipped_date, order_date)), 2) AS
avg_shipping_time
FROM products p
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE shipped_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_shipping_time
LIMIT 5;

SELECT m.merchant_name, COUNT(o.order_id) AS in_transit_orders


FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE o.delivery_status = 'In Transit'
GROUP BY m.merchant_name
ORDER BY in_transit_orders 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 c.customer_name, COUNT(o.order_id) AS pending_deliveries


FROM customers c
JOIN orders o USING(customer_id)
WHERE o.delivery_status IN ('In Transit', 'Pending')
GROUP BY c.customer_name;

SELECT p.product_name, COUNT(o.order_id) AS late_deliveries


FROM products p
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE o.delivery_status = 'Delayed'
GROUP BY p.product_name
ORDER BY late_deliveries DESC;
SELECT m.merchant_name, ROUND(AVG(DATEDIFF(delivery_date, shipped_date)),
2) AS avg_in_transit_time
FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE o.delivery_status = 'Delivered'
GROUP BY m.merchant_name;

SELECT order_id, customer_name, delivery_date, expected_delivery_date


FROM orders
JOIN customers USING(customer_id)
WHERE delivery_date = expected_delivery_date;

SELECT m.merchant_name, ROUND(AVG(DATEDIFF(shipped_date, order_date)), 2)


AS avg_shipping_time
FROM merchants m
JOIN products p USING(merchant_id)
JOIN order_details od USING(product_id)
JOIN orders o USING(order_id)
WHERE shipped_date IS NOT NULL
GROUP BY m.merchant_name
ORDER BY avg_shipping_time DESC;

SELECT customer_id, customer_name, ROUND(SUM(transaction_amount), 2) AS


total_spent
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT m.merchant_name, ROUND(AVG(t.transaction_amount), 2) AS


avg_transaction_value
FROM transactions t
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name;

SELECT transaction_date, COUNT(transaction_id) AS total_transactions


FROM transactions
GROUP BY transaction_date
ORDER BY transaction_date;

SELECT transaction_id, customer_id, merchant_id, transaction_amount


FROM transactions
ORDER BY transaction_amount DESC
LIMIT 10;
SELECT customer_id, customer_name,
ROUND(AVG(DATEDIFF(LEAD(transaction_date) OVER (PARTITION BY
customer_id ORDER BY transaction_date), transaction_date)), 2) AS
avg_days_between_transactions
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT merchant_id, merchant_name, COUNT(transaction_id) AS total_transactions


FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name;

SELECT transaction_date, merchant_id, merchant_name,


ROUND(SUM(transaction_amount), 2) AS daily_revenue
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY transaction_date, merchant_id, merchant_name;

SELECT customer_id, customer_name, COUNT(transaction_id) AS total_transactions


FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
ORDER BY total_transactions DESC;

SELECT merchant_id, merchant_name, DATE_FORMAT(transaction_date, '%Y-%m')


AS transaction_month, ROUND(SUM(transaction_amount), 2) AS monthly_revenue
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name, transaction_month
ORDER BY transaction_month;

SELECT merchant_id, merchant_name, MAX(transaction_amount) AS


highest_transaction
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name;
SELECT customer_id, customer_name, DATE_FORMAT(transaction_date, '%Y-%m')
AS transaction_month, COUNT(transaction_id) AS total_transactions
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name, transaction_month
ORDER BY transaction_month;

SELECT transaction_date, ROUND(SUM(transaction_amount), 2) AS total_revenue


FROM transactions
GROUP BY transaction_date
ORDER BY transaction_date;

SELECT customer_id, customer_name, ROUND(AVG(transaction_amount), 2) AS


avg_transaction_value
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
ORDER BY avg_transaction_value DESC;

SELECT COUNT(transaction_id) AS weekend_transactions


FROM transactions
WHERE DAYOFWEEK(transaction_date) IN (1, 7); -- 1 = Sunday, 7 = Saturday

SELECT merchant_id, merchant_name, ROUND(SUM(transaction_amount), 2) AS


total_revenue
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name
ORDER BY total_revenue DESC
LIMIT 5;

SELECT transaction_date, ROUND(AVG(transaction_amount), 2) AS


daily_avg_transaction
FROM transactions
GROUP BY transaction_date
ORDER BY transaction_date;

SELECT customer_id, customer_name,


MAX(DATEDIFF(LEAD(transaction_date) OVER (PARTITION BY customer_id
ORDER BY transaction_date), transaction_date)) AS
max_days_between_transactions
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;
SELECT merchant_id, merchant_name, COUNT(transaction_id) AS total_transactions
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name
ORDER BY total_transactions DESC;

SELECT customer_id, customer_name, MIN(transaction_date) AS first_transaction,


MAX(transaction_date) AS last_transaction
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT transaction_date, merchant_id, merchant_name, COUNT(transaction_id) AS


daily_transactions
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY transaction_date, merchant_id, merchant_name
ORDER BY transaction_date;

SELECT merchant_id, merchant_name, DATE_FORMAT(transaction_date, '%Y-%m')


AS transaction_month, ROUND(SUM(transaction_amount), 2) AS monthly_revenue
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name, transaction_month
ORDER BY transaction_month;

SELECT customer_id, customer_name, DATE_FORMAT(transaction_date, '%Y-%m')


AS transaction_month, COUNT(transaction_id) AS total_transactions
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name, transaction_month;

SELECT transaction_date, ROUND(AVG(transaction_amount), 2) AS


avg_transaction_value
FROM transactions
GROUP BY transaction_date;

SELECT customer_id, customer_name, ROUND(SUM(transaction_amount), 2) AS


total_spent
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
ORDER BY total_spent DESC
LIMIT 10;
SELECT merchant_id, merchant_name, COUNT(transaction_id) AS total_transactions
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name
ORDER BY total_transactions DESC;

SELECT transaction_date, COUNT(transaction_id) AS total_transactions


FROM transactions
GROUP BY transaction_date
ORDER BY transaction_date;

SELECT DATE_FORMAT(transaction_date, '%Y-%m') AS transaction_month,


ROUND(AVG(transaction_amount), 2) AS avg_transaction_amount
FROM transactions
GROUP BY transaction_month;

SELECT merchant_id, merchant_name, ROUND(SUM(transaction_amount), 2) AS


total_revenue
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name
ORDER BY total_revenue DESC;

SELECT transaction_date, COUNT(transaction_id) AS holiday_weekend_transactions


FROM transactions
WHERE DAYOFWEEK(transaction_date) IN (1, 7) -- 1 = Sunday, 7 = Saturday
GROUP BY transaction_date;

SELECT customer_id, customer_name, ROUND(AVG(transaction_amount), 2) AS


avg_transaction_value
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT DATE_FORMAT(transaction_date, '%Y-%m') AS transaction_month,


COUNT(transaction_id) AS total_transactions,
ROUND((COUNT(transaction_id) - LAG(COUNT(transaction_id)) OVER (ORDER BY
DATE_FORMAT(transaction_date, '%Y-%m'))) /
NULLIF(LAG(COUNT(transaction_id)) OVER (ORDER BY
DATE_FORMAT(transaction_date, '%Y-%m')), 0) * 100, 2) AS growth_rate
FROM transactions
GROUP BY transaction_month;
SELECT merchant_id, merchant_name,
ROUND((SUM(transaction_amount) - LAG(SUM(transaction_amount)) OVER
(PARTITION BY merchant_id ORDER BY DATE_FORMAT(transaction_date, '%Y-%m')))
/
NULLIF(LAG(SUM(transaction_amount)) OVER (PARTITION BY merchant_id
ORDER BY DATE_FORMAT(transaction_date, '%Y-%m')), 0) * 100, 2) AS
revenue_growth_rate
FROM transactions
JOIN merchants USING(merchant_id)
GROUP BY merchant_id, merchant_name, DATE_FORMAT(transaction_date, '%Y-
%m');

SELECT customer_id, customer_name, ROUND(STDDEV(SUM(transaction_amount))


OVER (PARTITION BY customer_id), 2) AS spending_consistency
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT order_id, customer_id, DATEDIFF(shipped_date, order_date) AS


shipping_delay
FROM orders
WHERE shipped_date IS NOT NULL
ORDER BY shipping_delay DESC
LIMIT 10;

SELECT m.merchant_name, ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)),


2) AS avg_shipping_time
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY m.merchant_name;

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, COUNT(o.order_id) AS in_transit_orders


FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
WHERE o.delivery_status = 'In Transit'
GROUP BY m.merchant_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 customer_id, customer_name, ROUND(SUM(transaction_amount), 2) AS


lifetime_spend
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT DATE_FORMAT(transaction_date, '%Y-%m') AS transaction_month,


ROUND(SUM(CASE WHEN refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(transaction_id) * 100, 2) AS monthly_refund_rate
FROM transactions
GROUP BY transaction_month;

SELECT product_id, product_name, ROUND(SUM(transaction_amount), 2) AS


total_revenue
FROM transactions
JOIN products USING(product_id)
GROUP BY product_id, product_name
ORDER BY total_revenue DESC
LIMIT 10;
SELECT m.merchant_name, ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)),
2) AS avg_delivery_time
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 customer_id, customer_name, COUNT(transaction_id) AS


transaction_count, ROUND(SUM(transaction_amount), 2) AS total_spend
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
HAVING total_spend > 10000;

SELECT DATE_FORMAT(order_date, '%Y-%m') AS order_month,


COUNT(CASE WHEN DATEDIFF(delivery_date, shipped_date) > 5 THEN order_id
END) AS delayed_shipments
FROM orders
GROUP BY order_month;

SELECT merchant_name, ROUND(AVG(DATEDIFF(delivery_date, order_date)), 2) AS


avg_delivery_days
FROM orders
JOIN products USING(product_id)
JOIN merchants USING(merchant_id)
WHERE delivery_date IS NOT NULL
GROUP BY merchant_name
ORDER BY avg_delivery_days;

SELECT customer_id, customer_name, DATEDIFF(CURRENT_DATE(),


MAX(transaction_date)) AS days_since_last_transaction
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
HAVING days_since_last_transaction > 180;

SELECT product_id, product_name, COUNT(order_id) AS total_returns


FROM orders
WHERE refund_status = 'Refunded'
GROUP BY product_id, product_name
ORDER BY total_returns DESC;
SELECT p.product_id, p.product_name,
ROUND(SUM(o.quantity) / AVG(p.stock_quantity), 2) AS inventory_turnover
FROM orders o
JOIN products p USING(product_id)
GROUP BY p.product_id, p.product_name;

SELECT transaction_date, ROUND(SUM(transaction_amount), 2) AS daily_revenue


FROM transactions
GROUP BY transaction_date
ORDER BY transaction_date;

SELECT m.merchant_id, 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_id, m.merchant_name
ORDER BY refund_rate DESC
LIMIT 5;

SELECT m.merchant_name, ROUND(AVG(DATEDIFF(o.delivery_date,


o.shipped_date)), 2) AS avg_in_transit_days
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;

SELECT DATE_FORMAT(transaction_date, '%Y-%m') AS transaction_month,


COUNT(DISTINCT customer_id) AS active_customers,
COUNT(DISTINCT CASE WHEN DATEDIFF(CURRENT_DATE(), transaction_date) >
180 THEN customer_id END) AS churned_customers,
ROUND(COUNT(DISTINCT CASE WHEN DATEDIFF(CURRENT_DATE(),
transaction_date) > 180 THEN customer_id END) / COUNT(DISTINCT customer_id) *
100, 2) AS churn_rate
FROM transactions
GROUP BY transaction_month;
SELECT p.product_id, p.product_name,
ROUND(SUM(o.quantity) / AVG(p.stock_quantity), 2) AS turnover_rate
FROM orders o
JOIN products p USING(product_id)
GROUP BY p.product_id, p.product_name
ORDER BY turnover_rate DESC
LIMIT 10;

SELECT customer_id, customer_name, ROUND(AVG(transaction_amount), 2) AS


avg_spend_per_order
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT m.merchant_name, COUNT(o.order_id) AS delayed_orders


FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
WHERE o.delivery_status = 'Delayed'
GROUP BY m.merchant_name;

SELECT c.customer_name, COUNT(o.order_id) AS refund_count


FROM orders o
JOIN customers c USING(customer_id)
WHERE o.refund_status = 'Refunded'
GROUP BY c.customer_name;

SELECT m.merchant_name, ROUND(STDDEV(DATEDIFF(o.delivery_date,


o.shipped_date)), 2) AS shipping_time_variability
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;

SELECT customer_id, customer_name, COUNT(order_id) AS total_orders


FROM orders
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
ORDER BY total_orders DESC;

SELECT customer_id, customer_name, COUNT(transaction_id) AS total_transactions


FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;
SELECT customer_id, customer_name, ROUND(AVG(transaction_amount), 2) AS
avg_transaction_amount
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT product_id, product_name, COUNT(order_id) AS total_orders


FROM orders
JOIN products USING(product_id)
GROUP BY product_id, product_name
ORDER BY total_orders DESC
LIMIT 1;

SELECT m.merchant_id, m.merchant_name, ROUND(SUM(t.transaction_amount), 2)


AS total_revenue
FROM transactions t
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_id, m.merchant_name
ORDER BY total_revenue DESC;

SELECT COUNT(order_id) AS on_time_orders


FROM orders
WHERE delivery_status = 'Delivered' AND DATEDIFF(delivery_date, order_date) <=
5;

SELECT customer_id, customer_name,


ROUND(SUM(CASE WHEN refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(order_id) * 100, 2) AS refund_percentage
FROM orders
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
ORDER BY refund_percentage ASC;

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;
SELECT product_id, product_name, ROUND(SUM(transaction_amount), 2) AS
total_revenue
FROM transactions
JOIN products USING(product_id)
GROUP BY product_id, product_name
ORDER BY total_revenue DESC;

SELECT DATE_FORMAT(order_date, '%Y-%m') AS order_month, COUNT(order_id) AS


total_refunds
FROM orders
WHERE refund_status = 'Refunded'
GROUP BY order_month;

SELECT m.merchant_name, COUNT(CASE WHEN delivery_status = 'Delivered' THEN


1 END) AS delivered_orders,
COUNT(CASE WHEN delivery_status = 'In Transit' THEN 1 END) AS
in_transit_orders,
COUNT(CASE WHEN delivery_status = 'Delayed' THEN 1 END) AS delayed_orders
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name;

SELECT customer_id, customer_name, COUNT(transaction_id) AS total_transactions


FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
ORDER BY total_transactions DESC
LIMIT 1;

SELECT customer_id, customer_name, ROUND(AVG(transaction_amount), 2) AS


avg_transaction_amount
FROM transactions
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name;

SELECT product_id, product_name, COUNT(order_id) AS total_orders


FROM orders
JOIN products USING(product_id)
GROUP BY product_id, product_name
ORDER BY total_orders DESC
LIMIT 1;
SELECT m.merchant_id, m.merchant_name, ROUND(SUM(t.transaction_amount), 2)
AS total_revenue
FROM transactions t
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_id, m.merchant_name
ORDER BY total_revenue DESC
LIMIT 1;

SELECT COUNT(order_id) AS on_time_orders


FROM orders
WHERE delivery_status = 'Delivered' AND DATEDIFF(delivery_date, order_date) <=
5;

SELECT customer_id, customer_name,


ROUND(SUM(CASE WHEN refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(order_id) * 100, 2) AS refund_percentage
FROM orders
JOIN customers USING(customer_id)
GROUP BY customer_id, customer_name
ORDER BY refund_percentage DESC
LIMIT 5;

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;

SELECT product_id, product_name, ROUND(SUM(transaction_amount), 2) AS


total_revenue
FROM transactions
JOIN products USING(product_id)
GROUP BY product_id, product_name
ORDER BY total_revenue DESC
LIMIT 10;

SELECT DATE_FORMAT(order_date, '%Y-%m') AS order_month, COUNT(order_id) AS


total_refunds
FROM orders
WHERE refund_status = 'Refunded'
GROUP BY order_month;
SELECT m.merchant_name,
COUNT(CASE WHEN delivery_status = 'Delivered' THEN 1 END) AS
delivered_orders,
COUNT(CASE WHEN delivery_status = 'In Transit' THEN 1 END) AS
in_transit_orders,
COUNT(CASE WHEN delivery_status = 'Delayed' THEN 1 END) AS delayed_orders
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name;

SELECT DATE_FORMAT(transaction_date, '%Y-%m') AS transaction_month,


ROUND(SUM(transaction_amount), 2) AS total_revenue
FROM transactions
GROUP BY transaction_month
ORDER BY transaction_month;

SELECT c.customer_id, c.customer_name, COUNT(o.order_id) AS refund_requests


FROM orders o
JOIN customers c USING(customer_id)
WHERE o.refund_status = 'Refunded'
GROUP BY c.customer_id, c.customer_name
ORDER BY refund_requests DESC
LIMIT 1;

SELECT p.product_id, p.product_name,


ROUND(AVG(DATEDIFF(o.delivery_date, o.shipped_date)), 2) AS
avg_delivery_time
FROM orders o
JOIN products p USING(product_id)
WHERE o.delivery_date IS NOT NULL AND o.shipped_date IS NOT NULL
GROUP BY p.product_id, p.product_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, COUNT(o.order_id) AS delayed_shipments


FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
WHERE o.delivery_status = 'Delayed'
GROUP BY m.merchant_name;

SELECT c.customer_id, c.customer_name,


ROUND(SUM(t.transaction_amount) / COUNT(o.order_id), 2) AS
avg_order_value
FROM transactions t
JOIN orders o USING(order_id)
JOIN customers c USING(customer_id)
GROUP BY c.customer_id, c.customer_name;

SELECT p.product_id, p.product_name,


ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS return_rate
FROM orders o
JOIN products p USING(product_id)
GROUP BY p.product_id, p.product_name
ORDER BY return_rate DESC
LIMIT 1;

SELECT COUNT(order_id) AS same_day_shipments


FROM orders
WHERE order_date = shipped_date;

SELECT c.customer_id, c.customer_name, ROUND(SUM(t.transaction_amount), 2)


AS total_spending
FROM transactions t
JOIN customers c USING(customer_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spending DESC
LIMIT 1;
SELECT product_id, product_name, ROUND(AVG(product_rating), 2) AS avg_rating
FROM orders
JOIN products USING(product_id)
GROUP BY product_id, product_name
ORDER BY avg_rating DESC
LIMIT 1;

SELECT m.merchant_name, ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)),


2) AS avg_delivery_time
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 c.customer_id, c.customer_name, ROUND(SUM(t.transaction_amount), 2)


AS total_spent
FROM transactions t
JOIN customers c USING(customer_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spent DESC
LIMIT 5;

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 DATE_FORMAT(order_date, '%Y-%m') AS order_month, COUNT(order_id) AS


in_transit_orders
FROM orders
WHERE delivery_status = 'In Transit'
GROUP BY order_month
ORDER BY order_month;

SELECT p.category, ROUND(SUM(t.transaction_amount), 2) AS total_revenue


FROM transactions t
JOIN products p USING(product_id)
GROUP BY p.category
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 refund_rate
FROM orders o
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name
ORDER BY refund_rate DESC;

SELECT customer_id, customer_name, COUNT(order_id) AS total_orders


FROM orders
GROUP BY customer_id, customer_name
HAVING total_orders > 1;

SELECT p.product_id, p.product_name, ROUND(AVG(t.transaction_amount), 2) AS


avg_transaction_amount
FROM transactions t
JOIN products p USING(product_id)
GROUP BY p.product_id, p.product_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 p.product_id, p.product_name, COUNT(o.order_id) AS total_refunds


FROM orders o
JOIN products p USING(product_id)
WHERE o.refund_status = 'Refunded'
GROUP BY p.product_id, p.product_name
ORDER BY total_refunds DESC
LIMIT 1;

SELECT p.category, ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2) AS


avg_delivery_time
FROM orders o
JOIN products p USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY p.category;
SELECT c.customer_id, c.customer_name, COUNT(o.order_id) AS total_orders
FROM orders o
JOIN customers c USING(customer_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY total_orders DESC
LIMIT 5;

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 p.product_id, p.product_name, ROUND(AVG(DATEDIFF(o.delivery_date,


o.shipped_date)), 2) AS avg_shipping_time
FROM orders o
JOIN products p USING(product_id)
WHERE o.delivery_date IS NOT NULL AND o.shipped_date IS NOT NULL
GROUP BY p.product_id, p.product_name
ORDER BY avg_shipping_time DESC
LIMIT 5;

SELECT delivery_status, COUNT(order_id) AS total_orders


FROM orders
GROUP BY delivery_status;

SELECT m.merchant_name, ROUND(SUM(t.transaction_amount), 2) AS


total_revenue
FROM transactions t
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY m.merchant_name
ORDER BY total_revenue DESC;

SELECT c.customer_id, c.customer_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 customers c USING(customer_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY refund_rate DESC
LIMIT 5;
SELECT DATE_FORMAT(order_date, '%Y-%m') AS order_month, COUNT(order_id) AS
in_transit_orders
FROM orders
WHERE delivery_status = 'In Transit'
GROUP BY order_month
ORDER BY order_month;

SELECT p.category, ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2) AS


avg_delivery_time
FROM orders o
JOIN products p USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY p.category
ORDER BY avg_delivery_time;

SELECT m.merchant_id, m.merchant_name, COUNT(o.order_id) AS total_orders,


ROUND(SUM(t.transaction_amount), 2) AS total_revenue
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_id, m.merchant_name
ORDER BY total_revenue DESC;

SELECT m.merchant_id, m.merchant_name, ROUND(AVG(o.product_rating), 2) AS


avg_rating
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_id, m.merchant_name
ORDER BY avg_rating DESC;

SELECT m.merchant_id, m.merchant_name, COUNT(DISTINCT o.customer_id) AS


unique_customers
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_id, m.merchant_name
ORDER BY unique_customers DESC;
SELECT p.product_id, p.product_name, 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 products p
JOIN merchants m USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY p.product_id, p.product_name, m.merchant_name
ORDER BY refund_rate 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 c.customer_id, c.customer_name, m.merchant_name,


ROUND(SUM(t.transaction_amount), 2) AS total_spent
FROM customers c
JOIN orders o USING(customer_id)
JOIN transactions t USING(order_id)
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY c.customer_id, c.customer_name, m.merchant_name
ORDER BY total_spent DESC;

SELECT p.product_name, m.merchant_name,


ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS avg_shipping_time
FROM products p
JOIN merchants m USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY p.product_name, m.merchant_name
ORDER BY avg_shipping_time;

SELECT m.merchant_name, DATE_FORMAT(t.transaction_date, '%Y-%m') AS month,


ROUND(SUM(t.transaction_amount), 2) AS monthly_revenue
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, month
ORDER BY month, monthly_revenue DESC;
SELECT c.customer_name, 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 customers c
JOIN orders o USING(customer_id)
JOIN products p USING(product_id)
JOIN merchants m USING(merchant_id)
GROUP BY c.customer_name, m.merchant_name
ORDER BY refund_rate DESC;

SELECT m.merchant_name, COUNT(o.order_id) AS in_transit_orders


FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'In Transit'
GROUP BY m.merchant_name
ORDER BY in_transit_orders DESC;

SELECT c.customer_id, c.customer_name, ROUND(AVG(DATEDIFF(o.delivery_date,


o.order_date)), 2) AS avg_delivery_time
FROM customers c
JOIN orders o USING(customer_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY c.customer_id, c.customer_name
ORDER BY avg_delivery_time;

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(AVG(t.transaction_amount), 2) AS


avg_transaction_amount
FROM products p
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY p.category
ORDER BY avg_transaction_amount DESC;
SELECT m.merchant_name, COUNT(o.order_id) AS total_refunds
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.refund_status = 'Refunded'
GROUP BY m.merchant_name
ORDER BY total_refunds DESC;

SELECT c.customer_id, c.customer_name, COUNT(o.order_id) AS total_orders,


ROUND(SUM(t.transaction_amount), 2) AS total_spent
FROM customers c
JOIN orders o USING(customer_id)
JOIN transactions t USING(order_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY total_orders DESC, total_spent;

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 p.product_name, ROUND(AVG(o.product_rating), 2) AS avg_rating


FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_rating DESC
LIMIT 10;

SELECT c.customer_name, ROUND(AVG(o.product_rating), 2) AS avg_rating,


COUNT(o.order_id) AS total_reviews
FROM customers c
JOIN orders o USING(customer_id)
WHERE o.product_rating IS NOT NULL
GROUP BY c.customer_name
HAVING total_reviews > 5
ORDER BY avg_rating DESC;
SELECT m.merchant_name, ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)),
2) AS avg_shipping_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_shipping_time;

SELECT p.category, 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.category
ORDER BY total_revenue DESC;

SELECT c.customer_id, c.customer_name, ROUND(AVG(t.transaction_amount), 2)


AS avg_order_value
FROM customers c
JOIN orders o USING(customer_id)
JOIN transactions t USING(order_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY avg_order_value DESC;

SELECT m.merchant_id, m.merchant_name, COUNT(o.order_id) AS


in_transit_orders
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'In Transit'
GROUP BY m.merchant_id, m.merchant_name
ORDER BY in_transit_orders DESC;

SELECT p.product_id, 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_id, p.product_name
ORDER BY refund_percentage DESC;
SELECT c.customer_id, c.customer_name, ROUND(AVG(DATEDIFF(o.delivery_date,
o.order_date)), 2) AS avg_delivery_time
FROM customers c
JOIN orders o USING(customer_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY c.customer_id, c.customer_name
ORDER BY avg_delivery_time DESC;

SELECT m.merchant_name, DATE_FORMAT(t.transaction_date, '%Y-%m') AS month,


ROUND(SUM(t.transaction_amount), 2) AS monthly_revenue
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, month
ORDER BY monthly_revenue DESC;

SELECT c.customer_id, c.customer_name,


ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_rate
FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY refund_rate DESC;

SELECT p.product_id, p.product_name,


ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS
avg_shipping_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY p.product_id, p.product_name
ORDER BY avg_shipping_time;

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(AVG(DATEDIFF(o.shipped_date, o.order_date)),


2) AS avg_shipping_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_shipping_time;

SELECT c.customer_id, c.customer_name, ROUND(SUM(t.transaction_amount), 2)


AS total_spent
FROM customers c
JOIN orders o USING(customer_id)
JOIN transactions t USING(order_id)
WHERE t.transaction_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spent DESC;

SELECT m.merchant_name, ROUND(AVG(o.product_rating), 2) AS avg_rating


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 avg_rating DESC;
SELECT p.product_name, COUNT(o.order_id) AS total_refunds
FROM products p
JOIN orders o USING(product_id)
WHERE o.refund_status = 'Refunded'
GROUP BY p.product_name
ORDER BY total_refunds DESC;

SELECT c.customer_id, c.customer_name, COUNT(o.order_id) AS late_deliveries


FROM customers c
JOIN orders o USING(customer_id)
WHERE o.delivery_status = 'Late'
GROUP BY c.customer_id, c.customer_name
ORDER BY late_deliveries 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 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;

SELECT c.customer_id, c.customer_name, p.product_name, COUNT(o.order_id) AS


repeat_purchases
FROM customers c
JOIN orders o USING(customer_id)
JOIN products p USING(product_id)
GROUP BY c.customer_id, c.customer_name, p.product_name
HAVING repeat_purchases > 1
ORDER BY repeat_purchases DESC;
SELECT m.merchant_name, DATE_FORMAT(o.order_date, '%Y-%m') AS month,
ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_rate
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name, month
ORDER BY refund_rate DESC;

SELECT c.customer_id, c.customer_name, ROUND(AVG(o.product_rating), 2) AS


avg_rating
FROM customers c
JOIN orders o USING(customer_id)
WHERE o.product_rating IS NOT NULL
GROUP BY c.customer_id, c.customer_name
ORDER BY avg_rating
LIMIT 10;

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(DATEDIFF(o.shipped_date, o.order_date)), 2)


AS avg_shipping_delay
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_shipping_delay DESC;

SELECT c.customer_id, c.customer_name, ROUND(AVG(DATEDIFF(o.delivery_date,


o.expected_delivery_date)), 2) AS avg_delivery_delay
FROM customers c
JOIN orders o USING(customer_id)
WHERE o.delivery_date > o.expected_delivery_date
GROUP BY c.customer_id, c.customer_name
ORDER BY avg_delivery_delay DESC;
SELECT m.merchant_name,
COUNT(o.order_id) AS total_orders,
SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) AS
total_refunds,
SUM(CASE WHEN o.delivery_status = 'In Transit' THEN 1 ELSE 0 END) AS
in_transit_orders
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name
ORDER BY total_orders 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 m.merchant_name, DATE_FORMAT(o.order_date, '%Y-%m') AS month,


COUNT(CASE WHEN o.delivery_status = 'Late' THEN o.order_id END) AS
late_deliveries
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name, month
ORDER BY month, late_deliveries DESC;
SELECT c.customer_id, c.customer_name, COUNT(DISTINCT o.product_id) AS
unique_products
FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_id, c.customer_name
ORDER BY unique_products DESC;

SELECT p.category, 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.category
ORDER BY total_revenue DESC;

SELECT c.customer_id, c.customer_name,


ROUND(SUM(CASE WHEN o.refund_status = 'Refunded' THEN 1 ELSE 0 END) /
COUNT(o.order_id) * 100, 2) AS refund_percentage
FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_id, c.customer_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_time
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_time DESC;

SELECT m.merchant_name, COUNT(DISTINCT o.product_id) AS


products_with_late_deliveries
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'Late'
GROUP BY m.merchant_name
ORDER BY products_with_late_deliveries DESC;
SELECT c.customer_id, c.customer_name,
ROUND(SUM(t.transaction_amount), 2) AS total_spent
FROM customers c
JOIN orders o USING(customer_id)
JOIN transactions t USING(order_id)
WHERE o.product_id IN (
SELECT product_id
FROM orders
WHERE refund_status = 'Refunded'
GROUP BY product_id
HAVING COUNT(order_id) > 5
)
GROUP BY c.customer_id, c.customer_name
ORDER BY total_spent DESC;

SELECT p.product_name, ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2)


AS avg_order_to_delivery
FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_order_to_delivery;

SELECT p.product_id, p.product_name, COUNT(o.order_id) AS total_orders


FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_id, p.product_name
ORDER BY total_orders DESC;

SELECT p.category, ROUND(AVG(o.product_rating), 2) AS avg_rating


FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.category
ORDER BY avg_rating 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_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(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2)


AS avg_shipping_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_shipping_time;

SELECT p.category, ROUND(AVG(DATEDIFF(o.delivery_date,


o.expected_delivery_date)), 2) AS avg_delivery_delay
FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date > o.expected_delivery_date
GROUP BY p.category
ORDER BY avg_delivery_delay DESC;

SELECT p.product_name, COUNT(o.order_id) AS late_deliveries


FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'Late'
GROUP BY p.product_name
ORDER BY late_deliveries DESC;

SELECT p.product_name, ROUND(AVG(o.product_rating), 2) AS avg_rating


FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_rating;

SELECT p.category, COUNT(o.order_id) AS in_transit_orders


FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'In Transit'
GROUP BY p.category
ORDER BY in_transit_orders 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, COUNT(DISTINCT o.customer_id) AS unique_customers


FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
ORDER BY unique_customers DESC;

SELECT p.product_name, ROUND(AVG(DATEDIFF(o.delivery_date, o.order_date)), 2)


AS avg_order_to_delivery
FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_order_to_delivery;

SELECT p.category, 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.category
ORDER BY refund_percentage DESC;

SELECT p.product_name, ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2)


AS avg_time_to_ship
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_time_to_ship DESC;

SELECT p.product_name, ROUND(AVG(t.transaction_amount), 2) AS


avg_transaction_value
FROM products p
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY p.product_name
ORDER BY avg_transaction_value DESC;
SELECT p.product_name, COUNT(o.order_id) AS refunded_orders
FROM products p
JOIN orders o USING(product_id)
WHERE o.refund_status = 'Refunded'
GROUP BY p.product_name
ORDER BY refunded_orders DESC;

SELECT p.category, ROUND(AVG(DATEDIFF(o.delivery_date,


o.expected_delivery_date)), 2) AS avg_delivery_delay
FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date > o.expected_delivery_date
GROUP BY p.category
ORDER BY avg_delivery_delay 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.category, COUNT(DISTINCT o.customer_id) AS unique_customers


FROM products p
JOIN orders o USING(product_id)
GROUP BY p.category
ORDER BY unique_customers DESC;

SELECT p.product_name, ROUND(AVG(t.transaction_amount), 2) AS


avg_order_value
FROM products p
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY p.product_name
ORDER BY avg_order_value 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;

SELECT p.category, 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.category
ORDER BY total_revenue DESC;

Which products experience the longest time between order and refund?

SELECT p.category, ROUND(AVG(t.transaction_amount), 2) AS


avg_transaction_amount
FROM products p
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY p.category
ORDER BY avg_transaction_amount DESC;

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(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS


avg_shipping_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY p.category
ORDER BY avg_shipping_time;

SELECT p.product_name, COUNT(o.complaint_id) AS total_complaints


FROM products p
JOIN orders o USING(product_id)
WHERE o.complaint_id IS NOT NULL
GROUP BY p.product_name
ORDER BY total_complaints DESC;
SELECT p.product_name, SUM(o.quantity) AS total_quantity_sold
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
ORDER BY total_quantity_sold DESC;

SELECT p.product_name, ROUND(AVG(DATEDIFF(o.refund_date,


o.refund_request_date)), 2) AS avg_refund_processing_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.refund_date IS NOT NULL AND o.refund_request_date IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_refund_processing_time;

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 p.product_name, COUNT(o.order_id) AS on_time_deliveries


FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'On Time'
GROUP BY p.product_name
ORDER BY on_time_deliveries DESC;

SELECT p.product_name, COUNT(t.transaction_id) AS transaction_count


FROM products p
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY p.product_name
ORDER BY transaction_count DESC;

SELECT p.product_name, COUNT(o.order_id) AS in_transit_orders


FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'In Transit'
GROUP BY p.product_name
ORDER BY in_transit_orders DESC;
SELECT p.category, ROUND(AVG(o.product_rating), 2) AS avg_customer_satisfaction
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.category
ORDER BY avg_customer_satisfaction DESC;

SELECT p.product_name, COUNT(o.complaint_id) AS late_delivery_complaints


FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_status = 'Late' AND o.complaint_id IS NOT NULL
GROUP BY p.product_name
ORDER BY late_delivery_complaints DESC;

SELECT p.category, ROUND(AVG(DATEDIFF(o.shipped_date, o.order_date)), 2) AS


avg_shipping_time
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipped_date IS NOT NULL
GROUP BY p.category
ORDER BY avg_shipping_time;

SELECT p.product_name, COUNT(o.order_id) AS early_deliveries


FROM products p
JOIN orders o USING(product_id)
WHERE o.delivery_date < o.expected_delivery_date
GROUP BY p.product_name
ORDER BY early_deliveries DESC;

SELECT p.product_name, ROUND(AVG(t.transaction_amount), 2) AS


avg_order_value
FROM products p
JOIN orders o USING(product_id)
JOIN transactions t USING(order_id)
GROUP BY p.product_name
ORDER BY avg_order_value;

SELECT p.product_name, ROUND(AVG(o.product_rating), 2) AS avg_product_rating


FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY avg_product_rating DESC;
SELECT m.merchant_name, ROUND(AVG(o.product_rating), 2) AS
avg_merchant_rating
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 avg_merchant_rating DESC;

SELECT p.product_name, COUNT(o.order_id) AS five_star_count


FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating = 5
GROUP BY p.product_name
ORDER BY five_star_count DESC;

SELECT m.merchant_name, o.product_rating, COUNT(o.order_id) AS rating_count


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, o.product_rating
ORDER BY m.merchant_name, o.product_rating;

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 p.product_name, COUNT(o.product_rating) AS total_reviews


FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.product_name
ORDER BY total_reviews DESC;
SELECT p.category, ROUND(AVG(o.product_rating), 2) AS avg_category_rating
FROM products p
JOIN orders o USING(product_id)
WHERE o.product_rating IS NOT NULL
GROUP BY p.category
ORDER BY avg_category_rating 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 m.merchant_name, ROUND(AVG(o.product_rating), 2) AS


avg_merchant_rating
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 avg_merchant_rating;

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 DATE_FORMAT(o.order_date, '%Y-%m') AS rating_month,


ROUND(AVG(o.product_rating), 2) AS avg_monthly_rating
FROM orders o
WHERE o.product_rating IS NOT NULL
GROUP BY rating_month
ORDER BY rating_month;

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 COUNT(CASE WHEN o.refund_requested = 'Yes' AND o.delivery_date >


o.estimated_delivery_date THEN 1 END) AS late_delivery_refunds,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.refund_requested = 'Yes' AND o.delivery_date >
o.estimated_delivery_date THEN 1 END) / COUNT(o.order_id) * 100, 2) AS
refund_percentage
FROM orders o
WHERE o.estimated_delivery_date IS NOT NULL AND o.delivery_date IS NOT NULL;
SELECT p.product_name,
COUNT(CASE WHEN o.shipping_status = 'In Transit' AND
DATEDIFF(o.delivery_date, o.shipping_date) > 5 THEN 1 END) AS delayed_in_transit,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.shipping_status = 'In Transit' AND
DATEDIFF(o.delivery_date, o.shipping_date) > 5 THEN 1 END) / COUNT(o.order_id) *
100, 2) AS delay_percentage
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipping_status IS NOT NULL AND o.delivery_date IS NOT NULL AND
o.shipping_date IS NOT NULL
GROUP BY p.product_name
ORDER BY delay_percentage 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 ROUND(AVG(DATEDIFF(o.delivery_date, o.shipping_date)), 2) AS


avg_shipping_time,
ROUND(AVG(o.product_rating), 2) AS avg_rating
FROM orders o
WHERE o.delivery_date IS NOT NULL AND o.shipping_date IS NOT NULL AND
o.product_rating IS NOT NULL
GROUP BY avg_shipping_time
ORDER BY avg_shipping_time;
SELECT p.product_name,
COUNT(CASE WHEN o.shipping_status = 'In Transit' AND
DATEDIFF(o.delivery_date, o.shipping_date) > 7 THEN 1 END) AS
long_transit_issues,
COUNT(o.order_id) AS total_orders,
ROUND(COUNT(CASE WHEN o.shipping_status = 'In Transit' AND
DATEDIFF(o.delivery_date, o.shipping_date) > 7 THEN 1 END) / COUNT(o.order_id) *
100, 2) AS issue_rate
FROM products p
JOIN orders o USING(product_id)
WHERE o.shipping_status IS NOT NULL AND o.shipping_date IS NOT NULL AND
o.delivery_date IS NOT NULL
GROUP BY p.product_name
ORDER BY issue_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 DATE_FORMAT(o.order_date, '%Y-%m') AS order_month,


COUNT(o.order_id) AS total_orders
FROM orders o
GROUP BY order_month
ORDER BY order_month;

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;

SELECT m.merchant_name, p.product_name, p.price


FROM merchants m
JOIN products p USING(merchant_id)
WHERE p.price > (SELECT AVG(price) FROM products);

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
HAVING total_spent > (SELECT AVG(total_amount) FROM orders);

SELECT p.product_name, COUNT(o.order_id) AS total_orders


FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
HAVING total_orders > (SELECT AVG(order_count)
FROM (SELECT COUNT(order_id) AS order_count
FROM orders
GROUP BY product_id) AS product_orders);

SELECT DISTINCT m.merchant_name


FROM merchants m
JOIN products p USING(merchant_id)
WHERE p.rating > (SELECT AVG(rating) FROM products WHERE rating IS NOT NULL);
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
HAVING total_spent > (SELECT MAX(total_amount) FROM orders);

SELECT p.product_name, COUNT(o.order_id) AS total_refunds


FROM products p
JOIN orders o USING(product_id)
WHERE o.refund_requested = 'Yes'
GROUP BY p.product_name
HAVING total_refunds > (SELECT AVG(refund_count)
FROM (SELECT COUNT(order_id) AS refund_count
FROM orders
WHERE refund_requested = 'Yes'
GROUP BY product_id) AS product_refunds);

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
HAVING total_revenue > (SELECT AVG(total_revenue)
FROM (SELECT SUM(total_amount) AS total_revenue
FROM orders
JOIN products USING(product_id)
GROUP BY merchant_id) AS merchant_revenues);

SELECT c.customer_name, COUNT(DISTINCT o.product_id) AS unique_products


FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_name
HAVING unique_products > (SELECT AVG(unique_products)
FROM (SELECT COUNT(DISTINCT product_id) AS unique_products
FROM orders
GROUP BY customer_id) AS customer_products);

SELECT product_name, price


FROM products
WHERE price > (SELECT MAX(price)
FROM products
WHERE category = 'Electronics');
SELECT m.merchant_name, COUNT(o.order_id) AS total_orders
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id)
GROUP BY m.merchant_name
HAVING total_orders > (SELECT MIN(order_count)
FROM (SELECT COUNT(order_id) AS order_count
FROM orders
JOIN products USING(product_id)
GROUP BY merchant_id) AS merchant_orders);

SELECT c.customer_name, ROUND(AVG(o.total_amount), 2) AS avg_order_value


FROM customers c
JOIN orders o USING(customer_id)
GROUP BY c.customer_name
HAVING avg_order_value > (SELECT AVG(total_amount) FROM orders);

SELECT p.product_name, p.category, p.rating


FROM products p
WHERE p.rating > (SELECT AVG(rating)
FROM products
WHERE category = p.category AND rating IS NOT NULL);

SELECT o.order_id, o.customer_id, o.total_amount


FROM orders o
WHERE o.total_amount > (SELECT AVG(total_amount)
FROM orders AS sub
WHERE sub.customer_id = o.customer_id);

SELECT o.shipping_status, COUNT(o.order_id) AS total_orders


FROM orders o
GROUP BY o.shipping_status
HAVING total_orders > (SELECT AVG(status_count)
FROM (SELECT COUNT(order_id) AS status_count
FROM orders
GROUP BY shipping_status) AS shipping_counts);
WITH ProductSales AS (
SELECT p.product_name, SUM(o.total_amount) AS total_sales
FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
)
SELECT product_name, total_sales
FROM ProductSales
ORDER BY total_sales DESC
LIMIT 5;

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);

SELECT m.merchant_name, o.order_date, SUM(o.total_amount)


OVER (PARTITION BY m.merchant_id ORDER BY o.order_date) AS running_total
FROM merchants m
JOIN products p USING(merchant_id)
JOIN orders o USING(product_id);

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;

SELECT customer_id, order_id, order_date,


RANK() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS
recent_order_rank
FROM orders
WHERE recent_order_rank = 1;

SELECT shipping_status, AVG(total_amount) AS avg_order_amount


FROM orders
GROUP BY shipping_status;

SELECT m.merchant_name, MAX(p.price) AS max_product_price


FROM merchants m
JOIN products p USING(merchant_id)
GROUP BY m.merchant_name
HAVING MAX(p.price) > (SELECT AVG(price) FROM products);

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 p.product_name, COUNT(o.order_id) AS total_orders


FROM products p
JOIN orders o USING(product_id)
GROUP BY p.product_name
ORDER BY total_orders DESC
LIMIT 3;
SELECT c.customer_name, COUNT(o.order_id) AS total_refunds
FROM customers c
JOIN orders o USING(customer_id)
WHERE o.refund_requested = 'Yes'
GROUP BY c.customer_name
ORDER BY total_refunds DESC
LIMIT 1;

SELECT merchant_id, product_name, rating


FROM (
SELECT p.merchant_id, p.product_name, p.rating,
RANK() OVER (PARTITION BY p.merchant_id ORDER BY p.rating DESC) AS
rating_rank
FROM products p
WHERE p.rating IS NOT NULL
) AS ranked_products
WHERE rating_rank = 1;

SELECT product_name, category, price,


price - (SELECT AVG(price) FROM products WHERE category = p.category) AS
price_difference
FROM products p;

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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy