fork download
  1. -- 1️⃣ Product টেবিল তৈরি
  2. CREATE TABLE Product (
  3. ProductID INT PRIMARY KEY,
  4. ProductName VARCHAR(100) NOT NULL,
  5. Category VARCHAR(50),
  6. Price DECIMAL(10,2) NOT NULL,
  7. Stock INT NOT NULL
  8. );
  9.  
  10. -- 2️⃣ Sell টেবিল তৈরি
  11. CREATE TABLE Sell (
  12. SellID INT PRIMARY KEY,
  13. ProductID INT,
  14. Quantity INT NOT NULL,
  15. SellDate DATE NOT NULL,
  16. FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
  17. );
  18.  
  19. -- 3️⃣ Product ডাটা ইনসার্ট
  20. INSERT INTO Product (ProductID, ProductName, Category, Price, Stock)
  21. VALUES
  22. (1, 'Smart T-Shirt', 'Clothing', 450.00, 50),
  23. (2, 'Smart Jeans', 'Clothing', 1200.00, 30),
  24. (3, 'Sneakers', 'Footwear', 2500.00, 20),
  25. (4, 'Cap', 'Accessories', 300.00, 100);
  26.  
  27. -- 4️⃣ Sell ডাটা ইনসার্ট
  28. INSERT INTO Sell (SellID, ProductID, Quantity, SellDate)
  29. VALUES
  30. (101, 1, 20, '2025-08-01'),
  31. (102, 1, 15, '2025-08-03'),
  32. (103, 2, 5, '2025-08-04'),
  33. (104, 3, 10, '2025-08-05'),
  34. (105, 4, 12, '2025-08-06'),
  35. (106, 4, 25, '2025-08-07');
  36.  
  37. -------------------------------------------------
  38. -- প্রশ্ন 2: Retrieve sales details for products with 'Smart' in their name
  39. -------------------------------------------------
  40. SELECT
  41. s.SellID,
  42. p.ProductName,
  43. s.Quantity,
  44. s.SellDate,
  45. (s.Quantity * p.Price) AS TotalPrice
  46. FROM Sell s
  47. JOIN Product p ON s.ProductID = p.ProductID
  48. WHERE p.ProductName LIKE '%Smart%';
  49.  
  50. -------------------------------------------------
  51. -- প্রশ্ন 3: Retrieve product name and total sales revenue for each product
  52. -------------------------------------------------
  53. SELECT
  54. p.ProductName,
  55. SUM(s.Quantity * p.Price) AS TotalRevenue
  56. FROM Product p
  57. JOIN Sell s ON p.ProductID = s.ProductID
  58. GROUP BY p.ProductName;
  59.  
  60. -------------------------------------------------
  61. -- প্রশ্ন 4: Categorize sales as High, Medium, or Low
  62. -------------------------------------------------
  63. SELECT
  64. s.SellID,
  65. p.ProductName,
  66. (s.Quantity * p.Price) AS TotalPrice,
  67. CASE
  68. WHEN (s.Quantity * p.Price) > 200 THEN 'High'
  69. WHEN (s.Quantity * p.Price) BETWEEN 100 AND 200 THEN 'Medium'
  70. ELSE 'Low'
  71. END AS SaleCategory
  72. FROM Sell s
  73. JOIN Product p ON s.ProductID = p.ProductID;
  74.  
  75. -------------------------------------------------
  76. -- প্রশ্ন 5: Calculate the number of days between current date and sale date
  77. -------------------------------------------------
  78. SELECT
  79. s.SellID,
  80. p.ProductName,
  81. s.SellDate,
  82. DATEDIFF(CURRENT_DATE, s.SellDate) AS DaysSinceSale
  83. FROM Sell s
  84. JOIN Product p ON s.ProductID = p.ProductID;
Success #stdin #stdout 0.03s 26044KB
stdin
Standard input is empty
stdout
-- 1️⃣ Product টেবিল তৈরি
CREATE TABLE Product (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(100) NOT NULL,
    Category VARCHAR(50),
    Price DECIMAL(10,2) NOT NULL,
    Stock INT NOT NULL
);

-- 2️⃣ Sell টেবিল তৈরি
CREATE TABLE Sell (
    SellID INT PRIMARY KEY,
    ProductID INT,
    Quantity INT NOT NULL,
    SellDate DATE NOT NULL,
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);

-- 3️⃣ Product ডাটা ইনসার্ট
INSERT INTO Product (ProductID, ProductName, Category, Price, Stock)
VALUES
(1, 'Smart T-Shirt', 'Clothing', 450.00, 50),
(2, 'Smart Jeans', 'Clothing', 1200.00, 30),
(3, 'Sneakers', 'Footwear', 2500.00, 20),
(4, 'Cap', 'Accessories', 300.00, 100);

-- 4️⃣ Sell ডাটা ইনসার্ট
INSERT INTO Sell (SellID, ProductID, Quantity, SellDate)
VALUES
(101, 1, 20, '2025-08-01'),
(102, 1, 15, '2025-08-03'),
(103, 2, 5, '2025-08-04'),
(104, 3, 10, '2025-08-05'),
(105, 4, 12, '2025-08-06'),
(106, 4, 25, '2025-08-07');

-------------------------------------------------
-- প্রশ্ন 2: Retrieve sales details for products with 'Smart' in their name
-------------------------------------------------
SELECT 
    s.SellID,
    p.ProductName,
    s.Quantity,
    s.SellDate,
    (s.Quantity * p.Price) AS TotalPrice
FROM Sell s
JOIN Product p ON s.ProductID = p.ProductID
WHERE p.ProductName LIKE '%Smart%';

-------------------------------------------------
-- প্রশ্ন 3: Retrieve product name and total sales revenue for each product
-------------------------------------------------
SELECT 
    p.ProductName,
    SUM(s.Quantity * p.Price) AS TotalRevenue
FROM Product p
JOIN Sell s ON p.ProductID = s.ProductID
GROUP BY p.ProductName;

-------------------------------------------------
-- প্রশ্ন 4: Categorize sales as High, Medium, or Low
-------------------------------------------------
SELECT 
    s.SellID,
    p.ProductName,
    (s.Quantity * p.Price) AS TotalPrice,
    CASE
        WHEN (s.Quantity * p.Price) > 200 THEN 'High'
        WHEN (s.Quantity * p.Price) BETWEEN 100 AND 200 THEN 'Medium'
        ELSE 'Low'
    END AS SaleCategory
FROM Sell s
JOIN Product p ON s.ProductID = p.ProductID;

-------------------------------------------------
-- প্রশ্ন 5: Calculate the number of days between current date and sale date
-------------------------------------------------
SELECT 
    s.SellID,
    p.ProductName,
    s.SellDate,
    DATEDIFF(CURRENT_DATE, s.SellDate) AS DaysSinceSale
FROM Sell s
JOIN Product p ON s.ProductID = p.ProductID;