๐ Question 88: Find Customers Who Purchased in Every Month of a Year
Table: orders (customer_id, order_date)
SELECT
customer_id
FROM orders
WHERE EXTRACT(YEAR FROM order_date) = 2025
GROUP BY customer_id
HAVING COUNT(
DISTINCT EXTRACT(MONTH FROM order_date)
) = 12;
๐ Question 89: Find the Most Frequently Bought Product After Product A
Table: order_items (order_id, product_id, sequence_no)
SELECT
b.product_id,
COUNT(*) AS purchase_count
FROM order_items a
JOIN order_items b
ON a.order_id = b.order_id
AND b.sequence_no = a.sequence_no + 1
WHERE a.product_id = 'Product_A'
GROUP BY b.product_id
ORDER BY purchase_count DESC
LIMIT 1;
๐ Question 90: Calculate Customer Lifetime in Days
Tables: customers (customer_id) orders (customer_id, order_date)
SELECT
customer_id,
MAX(order_date) - MIN(order_date) AS lifetime_days
FROM orders
GROUP BY customer_id;
โค๏ธ Double Tap For More
Post #2561
2.13K
- โค 5