0% found this document useful (0 votes)
2 views

Retailer Database

The document outlines a Retailer Database Management System implemented using SQL, which includes various tables for managing brands, products, customers, vendors, stores, and sales. It details the creation of the database, table definitions, data insertion, modifications for data integrity, and example SQL queries for analyzing sales data. The system is designed to facilitate retail operations and provide insights for decision-making.

Uploaded by

Abhinav Dev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Retailer Database

The document outlines a Retailer Database Management System implemented using SQL, which includes various tables for managing brands, products, customers, vendors, stores, and sales. It details the creation of the database, table definitions, data insertion, modifications for data integrity, and example SQL queries for analyzing sales data. The system is designed to facilitate retail operations and provide insights for decision-making.

Uploaded by

Abhinav Dev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Retailer Database Management System

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.

CREATE DATABASE retailer,

USE retailer;

Table Definitions

The database includes the following tables:

Brands: Stores information about different brands.

Product: Contains product details such as UPC, name, prices, dates, and more.
Product_Type: Defines product types and hierarchical relationships.

Vendor: Manages information about vendors.

Customer: Stores customer data including name, contact details, and address.

Store: Holds details of retail stores and their locations.

Inventory: Tracks product quantities in different stores.

Bill: Records sales transactions, including the products sold, prices, and dates.

Customer Bill: Establishes links between customers and their bills.


States: Manages states where stores are located.

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

drop database retailer;

create database retailer;

use retailer;

CREATE TABLE Brands (

brand_id INT AUTO_INCREMENT PRIMARY KEY,

brand_name VARCHAR(255) NOT NULL

);

CREATE TABLE Product (

product_id INT AUTO_INCREMENT PRIMARY KEY,


UPC VARCHAR(50) NOT NULL,

product_name VARCHAR(255) NOT NULL,

cost_price DECIMAL(10, 2) NOT NULL,

sell_price DECIMAL(10, 2) NOT NULL,

mfg_date DATE NOT NULL,

exp_date DATE NOT NULL,

weight DECIMAL(10, 2) NOT NULL,


product_description TEXT,

brand_id INT NOT NULL,

vendor_id INT NOT NULL,

FOREIGN KEY (brand_id) REFERENCES Brands(brand_id)

);

CREATE TABLE Product_Type (


type_id INT AUTO_INCREMENT PRIMARY KEY,

type_name VARCHAR(255) NOT NULL,


parent_id INT,

FOREIGN KEY (parent_id) REFERENCES Product_Type(type_id)

);

CREATE TABLE Vendor (

vendor_id INT AUTO_INCREMENT PRIMARY KEY,

vendor_name VARCHAR(255) NOT NULL,

mobile VARCHAR(15) NOT NULL

);

CREATE TABLE Customer (

customer_id INT AUTO_INCREMENT PRIMARY KEY,


customer_name VARCHAR(255) NOT NULL,

mobile VARCHAR(15) NOT NULL,

email VARCHAR(255),

address VARCHAR(255),

city VARCHAR(255),

pin VARCHAR(10)

);

CREATE TABLE Store (

store_id INT AUTO_INCREMENT PRIMARY KEY,

store_name VARCHAR(255),

address_l1 VARCHAR(255) NOT NULL,

address_l2 VARCHAR(255),

city VARCHAR(255) NOT NULL,


pin VARCHAR(10) NOT NULL

);
CREATE TABLE Inventory (

store_id INT,

product_id INT,

quantity INT NOT NULL,


PRIMARY KEY (store_id, product_id),

FOREIGN KEY (store_id) REFERENCES Store(store_id),

FOREIGN KEY (product_id) REFERENCES Product(product_id)

);

CREATE TABLE Bill (

bill_no INT AUTO_INCREMENT PRIMARY KEY,

product_id INT NOT NULL,


selling_price DECIMAL(10, 2) NOT NULL,

discount DECIMAL(10, 2) NOT NULL,

final_price DECIMAL(10, 2) NOT NULL,

store_id INT NOT NULL,

bill_date DATE NOT NULL,

FOREIGN KEY (product_id) REFERENCES Product(product_id),

FOREIGN KEY (store_id) REFERENCES Store(store_id)


);

CREATE TABLE Customer_Bill (

customer_id INT NOT NULL,

bill_no INT,

PRIMARY KEY (customer_id, bill_no),

FOREIGN KEY (customer_id) REFERENCES Customer(customer_id),


FOREIGN KEY (bill_no) REFERENCES Bill(bill_no)

);
INSERT INTO Brands (brand_name)

VALUES

('Parle Agro'),
('Tata Motors'),

('Amul'),

('Infosys'),

('Mahindra');

INSERT INTO Product (UPC, product_name, cost_price, sell_price, mfg_date, exp_date,


weight, product_description, brand_id, vendor_id)

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),

('4567890123', 'Software Services', 1500000, 1800000, '2022-03-01', '2024-03-01', 0.1, 'IT


consulting services', 4,3),

('5678901234', 'Mahindra Tractor', 600000, 700000, '2023-01-01', '2023-01-10', 1200,


'Agricultural tractor', 5,3);

INSERT INTO Product_Type (type_name, parent_id)

VALUES
('Beverages', NULL),

('Automobiles', NULL),

('Dairy', NULL),

('IT Services', NULL),

('Agricultural Equipment', NULL);

INSERT INTO Vendor (vendor_name, mobile)


VALUES

('Bisleri India', '9876543210'),

('Tata Motors Dealers', '8765432109'),

('Amul Distributors', '7654321098'),


('Infosys Solutions', '6543210987'),

('Mahindra Tractor Dealers', '5432109876');

INSERT INTO Customer (customer_name, mobile, email, address, city, pin)

VALUES

('Rajesh Sharma', '1234567890', '[email protected]', '123 Main Street', 'Mumbai', '400001'),

('Meena Singh', '2345678901', '[email protected]', '45 Park Avenue', 'Delhi', '110002'),

('Anand Verma', '3456789012', '[email protected]', '78 Gandhi Road', 'Kolkata', '700003'),


('Neha Kapoor', '4567890123', '[email protected]', '32 MG Road', 'Bangalore', '560004'),

('Amit Patel', '5678901234', '[email protected]', '56 Gandhi Nagar', 'Chennai', '600005');

INSERT INTO Store (store_name, address_l1, address_l2, city, pin)

VALUES

('Reliance Store', '1 Market Street', 'Gandhi Market', 'Mumbai', '400001'),

('Tata Supermart', '5 Mall Road', 'Shopping Center', 'Delhi', '110002'),


('Big Bazaar', '3 Mall Avenue', 'Shopping Plaza', 'Kolkata', '700003'),

('Spencers', '9 Brigade Road', 'Shopping Complex', 'Bangalore', '560004'),

('More Hypermarket', '2 Nelson Street', 'City Center', 'Chennai', '600005');

INSERT INTO Inventory (store_id, product_id, quantity)

VALUES

(1, 1, 500),
(2, 2, 50),

(3, 3, 1000),
(4, 4, 20),
(5, 5, 5);

INSERT INTO Bill (product_id, selling_price, discount, final_price, store_id, bill_date)

VALUES
(1, 20, 0, 20, 1, '2023-01-15'),

(2, 350000, 5000, 345000, 2, '2023-01-16'),

(3, 30, 0, 30, 3, '2023-01-17'),

(4, 1800000, 20000, 1780000, 4, '2023-01-18'),

(5, 700000, 10000, 690000, 5, '2023-01-19');

INSERT INTO Customer_Bill (customer_id, bill_no)

VALUES
(1, 1),

(2, 2),

(3, 3),

(4, 4),

(5, 5);

CREATE TABLE States (


state_id INT AUTO_INCREMENT PRIMARY KEY,

state_name VARCHAR(255) NOT NULL

);

INSERT INTO States (state_name)

VALUES
('Maharashtra'),

('Delhi'),
('West Bengal'),
('Karnataka'),

('Tamil Nadu');

ALTER TABLE Store ADD COLUMN state_id INT NOT NULL;


UPDATE Store SET state_id = 1 WHERE store_id IN (1, 2);

UPDATE Store SET state_id = 2 WHERE store_id = 3;

UPDATE Store SET state_id = 3 WHERE store_id = 4;

UPDATE Store SET state_id = 4 WHERE store_id = 5;

CREATE TABLE Sales (

sale_id INT AUTO_INCREMENT PRIMARY KEY,

store_id INT NOT NULL,


product_id INT NOT NULL,

sale_date DATE NOT NULL,

quantity_sold INT NOT NULL

);

INSERT INTO Sales (store_id, product_id, sale_date, quantity_sold)

VALUES
(1, 1, '2023-01-15', 50),

(2, 2, '2023-01-16', 10),

(3, 3, '2023-01-17', 500),

(4, 4, '2023-01-18', 5),

(5, 5, '2023-01-19', 2);

ALTER TABLE Customer_Bill


ADD FOREIGN KEY (customer_id) REFERENCES Customer(customer_id);

ALTER TABLE Product


ADD FOREIGN KEY (vendor_id) REFERENCES Vendor(vendor_id);

ALTER TABLE Store

ADD FOREIGN KEY (state_id) REFERENCES States(state_id);

ALTER TABLE Sales

ADD FOREIGN KEY (product_id) REFERENCES Product(product_id);

ALTER TABLE Product_Type

ADD FOREIGN KEY (parent_id) REFERENCES Product_Type(type_id);

Queries

a. What are the 20 top-selling products at each store?

SELECT s.store_name, p.product_name, SUM(quantity_sold) as total_sales

FROM Store s

JOIN Sales sa ON s.store_id = sa.store_id

JOIN Product p ON sa.product_id = p.product_id


GROUP BY s.store_name, p.product_name

ORDER BY s.store_name, total_sales DESC

LIMIT 20;
b. What are the 20 top-selling products in each state?

SELECT st.state_name, p.product_name, SUM(quantity_sold) as total_sales

FROM States st
JOIN Store s ON st.state_id = s.state_id

JOIN Sales sa ON s.store_id = sa.store_id

JOIN Product p ON sa.product_id = p.product_id

GROUP BY st.state_name, p.product_name

ORDER BY st.state_name, total_sales DESC

LIMIT 20;

c . What are the 5 stores with the most sales so far this year?

SELECT s.store_name, SUM(quantity_sold) as total_sales

FROM Store s

JOIN Sales sa ON s.store_id = sa.store_id

WHERE YEAR(sale_date) = YEAR(CURRENT_DATE())


GROUP BY s.store_name
ORDER BY total_sales DESC

LIMIT 5;

d . What are the 5 stores with the most sales so far this year?

SELECT COUNT(*) as stores_count

FROM (
SELECT store_id,

SUM(CASE WHEN p.product_name = 'Coke' THEN quantity_sold ELSE 0 END) as


coke_sales,

SUM(CASE WHEN p.product_name = 'Pepsi' THEN quantity_sold ELSE 0 END) as


pepsi_sales
FROM Sales sa

JOIN Product p ON sa.product_id = p.product_id

GROUP BY store_id

) sales_comparison

WHERE coke_sales > pepsi_sales;


e . What are the top 3 types of product that customers buy in addition to milk?

SELECT pt.type_name, COUNT(*) as type_count

FROM Product_Type pt
JOIN Product_Type ppt ON pt.type_id = ppt.type_id

JOIN Product p ON ppt.parent_id = p.product_id

WHERE pt.type_name != 'Dairy' AND p.product_name = 'Amul Milk'

GROUP BY pt.type_name

ORDER BY type_count DESC

LIMIT 3;

You might also like