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

CSDL Otck

ôn tập cuối kì csdl uet

Uploaded by

acctanzz1
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
8 views3 pages

CSDL Otck

ôn tập cuối kì csdl uet

Uploaded by

acctanzz1
Copyright
© © All Rights Reserved
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
You are on page 1/ 3

BÀI TẬP

Thông tin
Họ và tên Đào Ngọc Tân

MSSV 23021690

Tuần 10

Bài làm
Bài 1
SELECT
line.productLine productLine,
line.textDescription textDescription,
line.htmlDescription htmlDescription,
line.image image,
coalesce(no.TienHangTon, 0) TienHangTon
FROM
(SELECT
productLine,
SUM(quantityInStock * buyPrice) TienHangTon
FROM products
GROUP BY productline) no
RIGHT JOIN
productlines line
USING (productline);

Bài 2
(SELECT
pl.productLine productLine,
pl.textDescription textDescription,
SUM(o.quantityOrdered * o.priceEach) AS totalSold,
'Worst buy' AS note
FROM
products p
LEFT JOIN orderdetails o
ON p.productCode = o.productCode
LEFT JOIN productlines pl
ON p.productLine = pl.productLine
GROUP BY pl.productLine, pl.textDescription
ORDER BY totalSold ASC
LIMIT 1)
UNION ALL

(SELECT
pl.productLine productLine,
pl.textDescription textDescription,
SUM(o.quantityOrdered * o.priceEach) AS totalSold,
'Best buy' AS note
FROM
products p
LEFT JOIN orderdetails o
ON p.productCode = o.productCode
LEFT JOIN productlines pl
ON p.productLine = pl.productLine
GROUP BY pl.productLine, pl.textDescription
ORDER BY totalSold DESC
LIMIT 1)

Bài 3
SELECT
r.customerNumber,
r.customerName,
(r.TongTienHang - r.TongThanhToan) TienNo
FROM
(
SELECT
c.*,

( SELECT SUM(amount)
FROM payments p
WHERE p.customerNumber = c.customerNumber
) AS TongThanhToan,

( SELECT SUM((SELECT SUM(d.priceEach * d.quantityOrdered)


FROM orderdetails d
WHERE d.orderNumber = o.orderNumber) )
FROM orders o
WHERE o.customerNumber = c.customerNumber
) AS TongTienHang
FROM customers c
) AS r
WHERE r.TongTienHang - r.TongThanhToan > 50000

Bài 4
SELECT
productCode,
SUM(quantityOrdered) SoLuongHoanKho
FROM orderdetails
WHERE orderNumber in
( SELECT
orderNumber
FROM orders
WHERE status = 'Cancelled'
)
GROUP BY productCode

Bài 5
SET SQL_SAFE_UPDATES = 0;
UPDATE orders
SET comments = 'first order'
WHERE orderDate LIKE CONCAT((SELECT left(MIN(orderDate), 7) FROM
orders), '%');
SET SQL_SAFE_UPDATES = 1;

You might also like