Sure! Here are 15 SQL queries ranging from easy to complex, based on the example schema mentioned earlier. These queries will help you analyze sales, products, and customer data in various ways.
Easy Queries
1. Count Total Sales
SELECT COUNT(*) AS total_sales
FROM sales;
2. Calculate Total Revenue
SELECT SUM(quantity * price) AS total_revenue
FROM sales;
3. Count Unique Products Sold
SELECT COUNT(DISTINCT product_id) AS unique_products_sold
FROM sales;
4. Total Quantity Sold Per Product
SELECT product_id,
SUM(quantity) AS total_quantity
FROM sales
GROUP BY product_id
ORDER BY total_quantity DESC;
5. Total Sales by Customer
SELECT customer_id,
COUNT(*) AS total_sales
FROM sales
GROUP BY customer_id
ORDER BY total_sales DESC;
Intermediate Queries
6. Monthly Sales Revenue
SELECT DATE_FORMAT(sale_date, '%Y-%m') AS month,
SUM(quantity * price) AS total_revenue
FROM sales
GROUP BY month
ORDER BY month ASC;
7. Top 5 Products by Revenue
SELECT p.product_name,
SUM(s.quantity * s.price) AS total_revenue
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name
ORDER BY total_revenue DESC
LIMIT 5;
8. Average Discount on Sales
SELECT AVG(discount) AS average_discount
FROM sales;
9. Sales Information for Each Product
SELECT p.product_name,
COUNT(s.id) AS total_sales,
SUM(s.quantity * s.price) AS total_revenue
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name;
10. Customer Purchase Count by Year
SELECT YEAR(sale_date) AS year,
c.customer_name,
COUNT(*) AS total_purchases
FROM sales s
JOIN customers c ON s.customer_id = c.customer_id
GROUP BY year, c.customer_name
ORDER BY year, total_purchases DESC;
Advanced Queries
11. Yearly Revenue
SELECT YEAR(sale_date) AS year,
SUM(quantity * price) AS total_revenue
FROM sales
GROUP BY year
ORDER BY year ASC;
12. Customer Spending Analysis
SELECT c.customer_name,
SUM(s.quantity * s.price) AS total_spent,
COUNT(DISTINCT s.product_id) AS unique_products_purchased
FROM sales s
JOIN customers c ON s.customer_id = c.customer_id
GROUP BY c.customer_id
ORDER BY total_spent DESC;
13. Average Revenue Per Sale
SELECT AVG(quantity * price) AS average_revenue_per_sale
FROM sales;
14. Seasonal Sales Trends
SELECT MONTH(sale_date) AS month,
SUM(quantity * price) AS total_revenue
FROM sales
GROUP BY month
ORDER BY month ASC;
15. Top Customers by Revenue and Product Category
SELECT c.customer_name,
p.category,
SUM(s.quantity * s.price) AS total_spent
FROM sales s
JOIN customers c ON s.customer_id = c.customer_id
JOIN products p ON s.product_id = p.product_id
GROUP BY c.customer_id, p.category
ORDER BY total_spent DESC;
Notes
- Each query builds upon different aspects of the possible dataset, providing insights into sales performance, customer behavior, product performance, and seasonal trends.
- Ensure to adapt the column names and database dialect according to your actual schema and SQL dialect (e.g., MySQL, PostgreSQL, SQL Server).
Feel free to request additional queries or modifications that suit your specific analysis needs!