The document contains SQL commands for creating and managing a product and orders database. It includes queries to calculate total quantities, averages, maximums, minimums, and revenue related to products and orders. The document also provides insights into customer behavior and product performance through various aggregate functions.
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)
5 views6 pages
Aggregate Assignment SELF MADE
The document contains SQL commands for creating and managing a product and orders database. It includes queries to calculate total quantities, averages, maximums, minimums, and revenue related to products and orders. The document also provides insights into customer behavior and product performance through various aggregate functions.
values (1,1,2,2), (2,2,1,1), (3,3,3,3), (4,1,1,4), (5,3,2,2), (6,2,3,1); --1. What is the total quantity of products ordered? select sum(quantity) total_quantity from Orders --2. What is the average quantity of products ordered per order? select avg(quantity) avg_quantity from Orders
--3. What is the maximum quantity of products ordered?
select max(quantity) as max_quantity from Orders
--4. What is the minimum quantity of products ordered?
select min(quantity) as min_quantity from Orders
--5. How many orders have been placed?
select count(quantity) as total_orders from Orders
--6. What is the total price of products ordered?
SELECT SUM(o.quantity * p.price) AS total_price FROM Orders o JOIN Product p ON o.product_id = p.product_id; --7. What is the average price of products? select avg(price) avg_price from Product;
--8. What is the maximum price of a product?
select max(price) from Product
--9. What is the minimum price of a product?
select min(price) from Product
--10. How many customers have placed orders?
SELECT COUNT(DISTINCT customer_id) AS total_customers FROM Orders;
--11. What is the total quantity of each product
ordered? select p.product_name, sum(o.quantity) as total_quantity from orders o join product p on o.product_id = p.product_id group by p.product_name; --12. What is the average quantity of each product ordered? select product_name, avg(quantity) avg_quantity from orders o inner join Product p on o.product_id=p.product_id group by product_name
--13. What is the total price of products ordered by
each customer? select o.customer_id, sum(o.quantity * p.price) as total_spent from orders o join product p on o.product_id = p.product_id group by o.customer_id
--14. What is the average price of products ordered by
each customer? SELECT o.customer_id, AVG(p.price) AS average_price FROM Orders o JOIN Product p ON o.product_id = p.product_id GROUP BY o.customer_id; --15. What is the maximum quantity of products ordered by each customer? select customer_id, max(quantity) from Orders group by customer_id
--16. What is the minimum quantity of products ordered
by each customer? select customer_id, min(quantity) as min_quantity from orders group by customer_id;
--17. What is the total quantity of products ordered
per customer? SELECT customer_id, SUM(quantity) AS total_quantity FROM Orders GROUP BY customer_id; --18. What is the average price of products ordered per customer? select o.customer_id, avg(p.price) as average_price from orders o join product p on o.product_id = p.product_id group by o.customer_id;
--19. What is the total revenue generated from sales?
SELECT SUM(o.quantity * p.price) AS total_revenue FROM Orders o JOIN Product p ON o.product_id = p.product_id;
--20. What is the average quantity of products ordered
per customer? SELECT customer_id, AVG(quantity) AS average_quantity FROM Orders GROUP BY customer_id;