0% found this document useful (0 votes)
2 views2 pages

Ds

The document contains SQL queries for retrieving product names and total quantities sold, as well as the total number of orders for each customer. It also includes a query to find the top two customers by total profit. Additionally, it references MySQL documentation on view updatability.

Uploaded by

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

Ds

The document contains SQL queries for retrieving product names and total quantities sold, as well as the total number of orders for each customer. It also includes a query to find the top two customers by total profit. Additionally, it references MySQL documentation on view updatability.

Uploaded by

995aarvee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

--

-- list all product names and the total quantity sold

select productname, totalquantity


from products
inner join
(select productid, sum(quantity) as totalquantity
from orderitems
group by productid) as product_sold
on products.productid = product_sold.productid
;

select productname from products


where productid in
(select productid as totalquantity
from orderitems
group by productid);

the output of a subquery can be listed in the output of the main query
-- find the total number of orders for each customer

select firstname, lastname,


(select count(*) from orders
where orders.customerid = customers.customerid
) as no_of_orders
from customers
order by no_of_orders desc;

select count(*) from orders


where orders.customerid = customers.customerid;
select customer.Customer_ID, customer_name, TotalProfit
FROM
customer
inner join
(
SELECT Customer_ID, SUM(profit) AS TotalProfit
FROM market
GROUP BY Customer_ID
) AS CustomerProfits
on CustomerProfits.Customer_ID = customer.Customer_ID
ORDER BY TotalProfit DESC
LIMIT 2;

Task-3:
select customer.Customer_ID, customer_name, TotalProfit
FROM
customer
inner join
(
SELECT Customer_ID, SUM(profit) AS TotalProfit
FROM market
GROUP BY Customer_ID
) AS CustomerProfits
on CustomerProfits.Customer_ID = customer.Customer_ID
ORDER BY TotalProfit DESC
LIMIT 2;

https://dev.mysql.com/doc/refman/8.4/en/view-updatability.html

You might also like