0% found this document useful (0 votes)
16 views5 pages

Datawarehouse

The document shows SQL commands to create tables for customers, products, and sales with sample data and queries to retrieve information from the tables such as total sales by customer, average quantity of products sold, and customers who have not made a purchase.

Uploaded by

Joker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Datawarehouse

The document shows SQL commands to create tables for customers, products, and sales with sample data and queries to retrieve information from the tables such as total sales by customer, average quantity of products sold, and customers who have not made a purchase.

Uploaded by

Joker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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', '[email protected]'),
(2, 'Jane Smith', '[email protected]');

-- 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', '[email protected]'),
(2, 'Jane Smith', '[email protected]');

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