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.
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 ratings0% 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.
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);