Advertisement
Gaudenz

Performance 1-4

Sep 13th, 2024
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.66 KB | None | 0 0
  1. -- Step 1: Create the Database
  2. CREATE DATABASE retail_sales;
  3.  
  4. -- Step 2: Use the Database
  5. USE retail_sales;
  6.  
  7. -- Step 3: Create the Sales Table
  8. CREATE TABLE Sales (
  9.     SaleID INT PRIMARY KEY,
  10.     ProductName VARCHAR(50),
  11.     Category VARCHAR(50),
  12.     Region VARCHAR(50),
  13.     CustomerName VARCHAR(50),
  14.     TotalAmount DECIMAL(10, 2)
  15. );
  16.  
  17. -- Step 4: Insert Sample Data into the Sales Table
  18. INSERT INTO Sales (SaleID, ProductName, Category, Region, CustomerName, TotalAmount) VALUES
  19. (1, 'Laptop', 'Electronics', 'North', 'John Doe', 1500.00),
  20. (2, 'T-Shirt', 'Clothing', 'South', 'Jane Smith', 20.00),
  21. (3, 'Smartphone', 'Electronics', 'East', 'Alice Johnson', 800.00),
  22. (4, 'Shoes', 'Footwear', 'West', 'Michael Brown', 60.00),
  23. (5, 'Headphones', 'Electronics', 'North', 'Chris Lee', 120.00),
  24. (6, 'Dress', 'Clothing', 'South', 'Emma White', 45.00),
  25. (7, 'Tablet', 'Electronics', 'East', 'Sophia Miller', 600.00),
  26. (8, 'Sandals', 'Footwear', 'North', 'Oliver Green', 30.00),
  27. (9, 'Jacket', 'Clothing', 'West', 'Mia Taylor', 90.00),
  28. (10, 'Watch', 'Accessories', 'East', 'Liam Wilson', 150.00);
  29.  
  30. -- Step 5: Query to Find Top-Selling Products in Each Category
  31. SELECT Category, ProductName, SUM(TotalAmount) AS TotalSales
  32. FROM Sales
  33. GROUP BY Category, ProductName
  34. ORDER BY Category, TotalSales DESC;
  35.  
  36. -- Step 6: Query to Find Regions with the Highest Sales Revenue
  37. SELECT Region, SUM(TotalAmount) AS TotalRevenue
  38. FROM Sales
  39. GROUP BY Region
  40. ORDER BY TotalRevenue DESC;
  41.  
  42.  
  43. -- Step 7: Query to Find Top 5 Customers Based on Total Purchase Amount
  44. SELECT CustomerName, SUM(TotalAmount) AS TotalPurchase
  45. FROM Sales
  46. GROUP BY CustomerName
  47. ORDER BY TotalPurchase DESC
  48. LIMIT 5;
  49.  
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement