You are on page 1of 8

TUGAS ORACLE DB – CAHYA JULIAN

1. Create with DDL Statement (Supplier, Item, Transaction,


TransactionItem):
a. Item Code (Id) using ‘B’ for the first Letter.
b. Supplier Code (Id) using ‘SP’ for the first Letter.
c. Minimum Value for Quantity is 1.

Jawab:
CREATE TABLE suppliers (
supplier_id VARCHAR (50) PRIMARY KEY,
supplier_name VARCHAR (50),
join_date DATE);
CREATE TABLE items (
item_id VARCHAR(50) PRIMARY KEY,
item_name VARCHAR(50),
price INT,
stock INT,
supplier_id VARCHAR(50) NOT NULL,
FOREIGN KEY (supplier_id) REFERENCES suppliers
(supplier_id)
);
CREATE TABLE transactions(
transaction_id VARCHAR(50),
order_date DATE,
PRIMARY KEY (transaction_id)
);
CREATE TABLE transaction_item(
transaction_item_id INT PRIMARY KEY,
quantity INT,
transaction_id VARCHAR(50) NOT NULL,
item_id VARCHAR(50) NOT NULL,
FOREIGN KEY (transaction_id) REFERENCES transactions
(transaction_id),
FOREIGN KEY (item_id) REFERENCES items (item_id)
);

INSERT INTO transactions VALUES


('101', '2012-10-10'),
('102', '2012-10-15'),
('103', '2012-11-05'),
('104', '2012-11-20'),
('105', '2012-11-30');

INSERT INTO transaction_item VALUES


('01', '10', '101', 'B01'),
('02', '5', '101', 'B02'),
('03', '4', '102', 'B04'),
('04', '5', '103', 'B03'),
('05', '10', '104', 'B04'),
('06', '3', '105', 'B02');

a.
INSERT INTO items VALUES
('B01', 'Developer .Net', '6000000', '11', 'SP03'),
('B02', 'Developer Java', '6000000', '10', 'SP02'),
('B03', 'Developer C#', '5500000', '10', 'SP04'),
('B04', 'Developer Python', '5500000', '10', 'SP02');

b.
INSERT INTO suppliers VALUES
('SP01', 'PT. Synnex Metrodata Indonesia', '2018-01-15'),
('SP02', 'PT. Metrodata Indonesia', '2018-01-16'),
('SP03', 'PT. Mitra Informatika Indonesia', '2018-01-16'),
('SP04', 'PT. Metro Mobile', '2018-01-15');

c.
SELECT t.order_date, ti.quantity, i.item_name, i.price,
i.stock, s.supplier_name, s.join_date
FROM transactions as t
RIGHT JOIN transaction_item as ti
USING (transaction_id)
LEFT JOIN items as i
USING (item_id)
LEFT JOIN suppliers as s
USING (supplier_id)
WHERE quantity >= 1;
2. Show Data from table:
Jawab:

a.

SELECT i.item_id as kode_barang, i.item_name as nama_barang,


s.supplier_name as supplier
FROM items as i
LEFT JOIN suppliers as s
USING (supplier_id);

b.

SELECT t.transaction_id as nomor_faktur, s.supplier_name as


supplier,
i.item_name as barang, i.price
FROM transactions as t
RIGHT JOIN transaction_item
USING (transaction_id)
LEFT JOIN items as i
USING (item_id)
LEFT JOIN suppliers as s
USING (supplier_id) WHERE transaction_id = '101';

C.

SELECT t.transaction_id as nomor_faktur, i.item_name as


barang,
ti.quantity, i.price as harga, SUM(quantity * price) as
total_harga
FROM transactions as t
RIGHT JOIN transaction_item as ti
USING (transaction_id)
LEFT JOIN items as i
USING (item_id)
LEFT JOIN suppliers as s
USING (supplier_id)
GROUP BY transaction_item_id ;

3. Show data from table and add due date 14 days from Order
Date.
Jawab:
SELECT t.transaction_id as nomor_faktur,SUM(quantity*price)
as total_faktur, t.order_date as tanggal_faktur
, date_add(t.order_date, INTERVAL 14 DAY) as jatuh_tempo
FROM transactions as t
INNER JOIN transaction_item as ti
USING (transaction_id)
LEFT JOIN items
USING (item_id)
GROUP BY transaction_id;

4. (a) Show data total product has been sold and


(b) Show data total product has been sold in October
Jawab:

a.

SELECT i.item_name as name, sum(quantity) as quantity


FROM items as i
RIGHT JOIN transaction_item
USING (item_id)
GROUP BY item_id;

b.

SELECT i.item_name as name, sum(quantity) as quantity


FROM transactions
RIGHT JOIN transaction_item
USING (transaction_id)
LEFT JOIN items as i
USING (item_id)
WHERE order_date <= '2012-11-01'
GROUP BY item_id;

5. Show data Product has been sold in October, November and


December
Jawab:
select i.item_name as name
,ifnull(sum(case when month(order_date) = 10 then quantity
end), 0) Oktober
,ifnull(sum(case when month(order_date) = 11 then quantity
end), 0) November
,ifnull(sum(case when month(order_date) = 12 then quantity
end), 0) Desember
FROM transactions
RIGHT JOIN transaction_item
USING (transaction_id)
LEFT JOIN items as i
USING (item_id)
GROUP BY item_name;

6. Show data Product has been sold in October, November and


December. Show Total for all Month.
Jawab:

(select i.item_name as name


,ifnull(sum(case when month(order_date) = 10 then quantity
end), 0) Oktober
,ifnull(sum(case when month(order_date) = 11 then quantity
end), 0) November
,ifnull(sum(case when month(order_date) = 12 then quantity
end), 0) Desember
FROM transactions
RIGHT JOIN transaction_item
USING (transaction_id)
LEFT JOIN items as i
USING (item_id)
GROUP BY item_name)
UNION
(SELECT "T O T A L" as name
,ifnull(sum(case when month(order_date) = 10 then quantity
end), 0) Oktober
,ifnull(sum(case when month(order_date) = 11 then quantity
end), 0) November
,ifnull(sum(case when month(order_date) = 12 then quantity
end), 0) Desember
FROM transactions
RIGHT JOIN transaction_item
USING (transaction_id)
LEFT JOIN items as i
USING (item_id));

You might also like