Query
Query
SELECT
SUM(price) AS total_pendapatan,
SUM(price-cost)
FROM items;
--LATIHAN 2
SELECT
name,
price,
FROM items
WHERE price <=20 AND gender = 2
ORDER BY price DESC;
--LATIHAN 3
user_id AS pelanggan,
COUNT(item_id) AS jumlah_belanja
FROM sales_records
WHERE purchased_at LIKE '%2018-07%'
GROUP BY user_id
ORDER BY COUNT(item_id) DESC
LIMIT 5;
--LATIHAN 4
SELECT
user_id AS customer,
AVG(price) AS average_shopping_amount
FROM sales_records
JOIN items
ON sales_records.item_id = item.id
GROUP BY user_id
HAVING purchased_at LIKE '%2018-07%'
AND AVG(price) >= 100
ORDER BY AVG(price) DESC
LIMIT 5;
--LATIHAN 5
SELECT
gender,
SUM(price) AS total_pendapatan,
SUM(price-cost) AS total_laba
FROM sales_records
JOIN items
ON sales_records.item_id = items.id
GROUP BY gender;
--LATIHAN 6
SELECT
name,
COUNT(item_id) AS total_penjualan,
price,
price*COUNT(item_id) AS total_pendapatan
FROM items
JOIN sales_records
ON items.id = sales_records.item_id
GROUP BY item_id
ORDER BY price DESC
LIMIT 5;