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

DBCK

Uploaded by

tintinthanhthanh
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)
8 views2 pages

DBCK

Uploaded by

tintinthanhthanh
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

03,

select productLine, sum(quantityInStock) total


from products
group by productLine
order by total desc

4,
SELECT
pl.productLine,
pl.textDescription,
sum(p.quantityInStock) AS totalInStock,
sum(
(select SUM(od.quantityOrdered)
FROM orderdetails od
WHERE od.productCode = p.productCode
)
) AS totalSold,
SUM(p.quantityInStock) + SUM(
(SELECT SUM(od.quantityOrdered)
FROM orderdetails od
WHERE od.productCode = p.productCode
)
) AS totalProducts
FROM
productlines pl
JOIN
products p ON p.productLine = pl.productLine
GROUP BY
pl.productLine, pl.textDescription
having
(SUM(
(SELECT sum(od.quantityOrdered)
FROM orderdetails od
WHERE od.productCode = p.productCode
)
) / nullif(sum(p.quantityInStock), 0)) >= 0.3;

5,
select customerName, (
select sum(quantityOrdered * priceEach)
from orderdetails
where orderNumber in (
select orderNumber
from orders
where customerNumber = customers.customerNumber
)
) totalBuy, (
select sum(amount)
from payments
where customerNumber = customers.customerNumber
) totalPay, (
select totalBuy - totalPay
) totalDebt, (
select totalDebt / totalBuy
) ratio
from customers
having ratio > 0.5
6,

select productName, (
select sum(quantityOrdered)
from orderdetails
where orderNumber in (
select orderNumber
from orders
where productCode = products.productCode and status = "cancelled"
)
) total
from products
having total is not null
order by total

You might also like