Retailer Database
Retailer Database
This is a Retailer Database Management System implemented using SQL. The system
comprises various tables to store information related to brands, products, customers, vendors,
stores, sales, and more. The database is designed to manage retail operations and provide
insights into sales and inventory.
First, we create the database named "retailer" and select it for all further operations.
USE retailer;
Table Definitions
Product: Contains product details such as UPC, name, prices, dates, and more.
Product_Type: Defines product types and hierarchical relationships.
Customer: Stores customer data including name, contact details, and address.
Bill: Records sales transactions, including the products sold, prices, and dates.
Sales: Captures data about products sold, including store, product, date, and quantity.
Data Insertion
Data is inserted into the various tables to populate the database with sample information. This
includes data for brands, products, product types, vendors, customers, stores, inventory, bills,
and more.
Data Modifications
The database schema is modified to add foreign key constraints and ensure data integrity.
Alterations include:
Adding a "state_id" column to the Store table and updating values based on the store's location.
Adding foreign keys to establish relationships between tables, such as Customer_Bill to
Customer, Product to Vendor, Store to States, Sales to Product, and Product_Type to itself.
Several SQL queries are provided for analyzing the data in the database:
Total Sales by Store and Product: Retrieve the total sales of products by store, sorted by store
and total sales.
Total Sales by State and Product: Retrieve the total sales of products by state, sorted by state
and total
sales.
Total Sales by Store for the Current Year: Retrieve the total sales by store for the current year.
Comparison of Coke and Pepsi Sales: Compare sales of "Coke" and "Pepsi" by store, and find
stores where Coke outsells Pepsi.
Top Product Types Related to "Amul Milk": Find the top three product types related to "Amul
Milk" (excluding "Dairy").
Conclusion
This README provides an overview of the Retailer Database Management System, its tables,
data, modifications, and example queries. It serves as a foundation for managing retail
operations and extracting valuable insights for decision-making.
ER-DIAGRAM
SCHEMA
Queries
use retailer;
);
);
);
);
email VARCHAR(255),
address VARCHAR(255),
city VARCHAR(255),
pin VARCHAR(10)
);
store_name VARCHAR(255),
address_l2 VARCHAR(255),
);
CREATE TABLE Inventory (
store_id INT,
product_id INT,
);
bill_no INT,
);
INSERT INTO Brands (brand_name)
VALUES
('Parle Agro'),
('Tata Motors'),
('Amul'),
('Infosys'),
('Mahindra');
VALUES
('1234567890', 'Mineral Water', 10, 20, '2023-01-01', '2023-12-31', 0.5, 'Bottled mineral water',
1,1),
('2345678901', 'Nano Car', 300000, 350000, '2022-06-01', '2025-06-01', 800, 'Compact car',
2,2),
('3456789012', 'Amul Milk', 25, 30, '2022-01-01', '2023-12-31', 1, 'Pasteurized cow milk', 3,2),
VALUES
('Beverages', NULL),
('Automobiles', NULL),
('Dairy', NULL),
VALUES
VALUES
VALUES
(1, 1, 500),
(2, 2, 50),
(3, 3, 1000),
(4, 4, 20),
(5, 5, 5);
VALUES
(1, 20, 0, 20, 1, '2023-01-15'),
VALUES
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5);
);
VALUES
('Maharashtra'),
('Delhi'),
('West Bengal'),
('Karnataka'),
('Tamil Nadu');
);
VALUES
(1, 1, '2023-01-15', 50),
Queries
FROM Store s
LIMIT 20;
b. What are the 20 top-selling products in each state?
FROM States st
JOIN Store s ON st.state_id = s.state_id
LIMIT 20;
c . What are the 5 stores with the most sales so far this year?
FROM Store s
LIMIT 5;
d . What are the 5 stores with the most sales so far this year?
FROM (
SELECT store_id,
GROUP BY store_id
) sales_comparison
FROM Product_Type pt
JOIN Product_Type ppt ON pt.type_id = ppt.type_id
GROUP BY pt.type_name
LIMIT 3;