The document contains 12 SQL queries related to analyzing sales data from an online store. The queries return information like the top selling products, average income of customers who purchased a specific product, customers who have never ordered in December 2004, and countries with customers who purchased at least twice. The last query adds a new column to the orderlines table to store the amount paid for each product in an order.
The document contains 12 SQL queries related to analyzing sales data from an online store. The queries return information like the top selling products, average income of customers who purchased a specific product, customers who have never ordered in December 2004, and countries with customers who purchased at least twice. The last query adds a new column to the orderlines table to store the amount paid for each product in an order.
MSSV: 20215007 Lớp: 139353 1. Display top 10 best-revenue products (the best- revenue products mean that the products give the highest revenue; revenu = quantity * price) select p.title, sum(ol.quantity * p.price) as revenue from products p join orderlines ol on p.prod_id = ol.prod_id group by p.prod_id, p.title order by revenue desc limit 10; 2. The average income of the customers who purchased the product titled “AIRPORT ROBBERS” select avg(c.income) as average_income from customers c join orders o on c.customerid = o.customerid join orderlines ol on o.orderid = ol.orderid join products p on ol.prod_id = p.prod_id where p.title = 'AIRPORT ROBBERS'; 3. Give a list of customers who have never ordered any product in December 2004. select c.customerid, c.firstname, c.lastname from customers c where not exists ( select 1 from orders o where c.customerid = o.customerid and o.orderdate >= '2004-12-01'::date and o.orderdate < '2005-01-01'::date ); 4. Give a list of the country names, their number of customers who have purchased at least 2 times select c.country, count(distinct c.customerid) as customer_count from customers c join orders o on c.customerid = o.customerid group by c.country having count(distinct o.orderid) >= 2; 5. Please list the orders in which both products are ordered titled “ADAPTATION SECRETS” and “AFFAIR GENTLEMENT” select o.orderid from orders o join orderlines ol1 on o.orderid = ol1.orderid join orderlines ol2 on o.orderid = ol2.orderid join products p1 on ol1.prod_id = p1.prod_id join products p2 on ol2.prod_id = p2.prod_id where p1.title = 'ADAPTATION SECRETS' and p2.title = 'AFFAIR GENTLEMENT'; 6. Show detailed information of products in the latest order: orderlineid, prod_id, product title, quantity, unit price (with currency unit), amount (with currency unit) select ol.orderlineid, p.prod_id, p.title as product_title, ol.quantity, concat(p.price, ' USD') AS unit_price, concat((ol.quantity * p.price), ' USD') AS amount from orderlines ol join products p ON ol.prod_id = p.prod_id where ol.orderid = ( select orderid from orders order by orderdate desc limit 1 ); 7. Display the maximum, minimum and average price of the products in the store select max(price) as max_price, min(price) as min_price, avg(price) as avg_price from products; 8. List of products that have been ordered by the current date. select p.title as product_title from products p join orderlines ol on p.prod_id = ol.prod_id join orders o on ol.orderid = o.orderid where o.orderdate = current_date; 9. Provide a list of female customers whose income is at least 10000 select firstname, lastname, income from customers where gender = 'F' and income >= 10000; 10. Display a list of the most expensive products that have been purchased by a male customer select p.title as product_title, p.price from products p join orderlines ol on p.prod_id = ol.prod_id join orders o on ol.orderid = o.orderid join customers c on o.customerid = c.customerid where c.gender = 'M' order by p.price desc limit 10; 11. Please indicate the number of orders that each customer has ordered. The list must contain customer ID, customer fullname, number of orders. Sort in descending order of the number of orders. select c.customerid, concat(c.firstname, ' ', c.lastname) as fullname, count(o.orderid) as num_orders from customers c join orders o on c.customerid = o.customerid group by c.customerid, fullname order by num_orders DESC; 12. Add a new column “amount” (number) into the table “orderlines” to store the amount paid for the corresponding product in this order. Write a SQL statement to update the correct value for this columns alter table orderlines add column amount numeric; update orderlines ol set amount = ol.quantity * p.price from products p where ol.prod_id = p.prod_id;