You are on page 1of 5

-- Create Customers Table

CREATE TABLE IF NOT EXISTS customers (


customer_id INT,
customer_name VARCHAR(255),
customer_email VARCHAR(255), -- Adjust the length as needed
PRIMARY KEY (customer_id)
);

-- Create Products Table


CREATE TABLE IF NOT EXISTS products (
product_id INT,
product_name VARCHAR(255),
product_price DECIMAL(10, 2),
PRIMARY KEY (product_id)
);

-- Create Sales Table


CREATE TABLE IF NOT EXISTS sales (
sale_id INT,
customer_id INT,
product_id INT,
sale_date DATE,
quantity INT,
total_amount DECIMAL(10, 2),
PRIMARY KEY (sale_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
-- Insert Sample Data into Customers Table
INSERT INTO customers VALUES
(1, 'John Doe', 'john.doe@email.com'),
(2, 'Jane Smith', 'jane.smith@email.com');

-- Insert Sample Data into Products Table


INSERT INTO products VALUES
(101, 'Product A', 50.00),
(102, 'Product B', 75.50);

-- Insert Sample Data into Sales Table


INSERT INTO sales VALUES
(1001, 1, 101, '2024-03-01', 2, 100.00),
(1002, 2, 102, '2024-03-02', 3, 226.50);
-- Create Customers Table
CREATE TABLE IF NOT EXISTS customers (
customer_id INT,
customer_name VARCHAR(255),
customer_email VARCHAR(255),
PRIMARY KEY (customer_id)
);

-- Create Products Table


CREATE TABLE IF NOT EXISTS products (
product_id INT,
product_name VARCHAR(255),
product_price DECIMAL(10, 2),
PRIMARY KEY (product_id)
);

-- Create Sales Table


CREATE TABLE IF NOT EXISTS sales (
sale_id INT,
customer_id INT,
product_id INT,
sale_date DATE,
quantity INT,
total_amount DECIMAL(10, 2),
PRIMARY KEY (sale_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);

-- Insert Sample Data into Customers Table


INSERT INTO customers VALUES
(1, 'John Doe', 'john.doe@email.com'),
(2, 'Jane Smith', 'jane.smith@email.com');

-- Insert Sample Data into Products Table


INSERT INTO products VALUES
(101, 'Product A', 50.00),
(102, 'Product B', 75.50);

-- Insert Sample Data into Sales Table


INSERT INTO sales VALUES
(1001, 1, 101, '2024-03-01', 2, 100.00),
(1002, 2, 102, '2024-03-02', 3, 226.50);

-- Additional Queries

-- 1. Display all products:


SELECT * FROM products;

-- 2. Show sales with customer details:


SELECT s.sale_id, c.customer_name, s.sale_date, s.total_amount
FROM sales s
JOIN customers c ON s.customer_id = c.customer_id;

-- 3. List sales with product details:


SELECT s.sale_id, p.product_name, s.quantity, s.total_amount
FROM sales s
JOIN products p ON s.product_id = p.product_id;

-- 4. Count the number of products:


SELECT COUNT(*) AS product_count FROM products;

-- 5. Find customers who made a purchase in a specific date range:


SELECT DISTINCT c.customer_name
FROM customers c
JOIN sales s ON c.customer_id = s.customer_id
WHERE s.sale_date BETWEEN '2024-03-01' AND '2024-03-31';

-- 6. Identify sales with high quantities (quantity > 2):


SELECT sale_id, quantity
FROM sales
WHERE quantity > 2;

-- 7. Display the total sales amount for each customer:


SELECT c.customer_name, SUM(s.total_amount) AS total_sales
FROM customers c
JOIN sales s ON c.customer_id = s.customer_id
GROUP BY c.customer_name;

-- 8. Find the average quantity of products sold:


SELECT AVG(quantity) AS avg_quantity FROM sales;

-- 9. Show the earliest sale date for each product:


SELECT p.product_name, MIN(s.sale_date) AS earliest_sale_date
FROM products p
JOIN sales s ON p.product_id = s.product_id
GROUP BY p.product_name;

-- 10. Display customers who have not made a purchase:


SELECT customer_name
FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM sales);

You might also like