Quiz
Quiz
D:\laragon\www
λ mysql -u root -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.30 MySQL Community Server - GPL
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> INSERT INTO npm_trans (Id, FirstName, LastName, City, Country, Phone) VALUES
(1, 'Maria', 'Anders', 'Berlin', 'Germany', '030-0074321'), (2, 'Ana', 'Trujillo',
'México D.F', 'Mexico', '(5) 555-4729'), (3, 'Antonio', 'Moreno', 'México D.F',
'Mexico', '(5) 555-7788'), (4, 'Thomas', 'Hardy', 'London', 'UK', '0921-12 34 65'),
(5, 'Christina', 'Berglund', 'Luleå', 'Sweden', '0621-08460'), (6, 'Hanna', 'Moos',
'Mannheim', 'Germany', '0621-08460'), (7, 'Frédérique', 'Côteaux', 'Strasbourg',
'France', '88.60.15.31'), (8, 'Martin', 'Sommer', 'Madrid', 'Spain', '(91) 555 22
82');
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> INSERT INTO products (id, ProductName, UnitPrice, Package) VALUES (1,
'Chai', 18.00, '10 boxes x 20 bags'), (2, 'Chang', 19.00, '24 - 12 oz bottles'),
(3, 'Aniseed Syrup', 10.00, '12 - 550 ml bottles'), (4, 'Chef Anton\'s Cajun
Seasoning', 22.00, '48 - 6 oz jars'), (5, 'Chef Anton\'s Gumbo Mix', 21.35, '36
boxes'), (6, 'Grandma\'s Boysenberry Spread', 25.00, '12 - 8 oz jars'), (7, 'Uncle
Bob\'s Organic Dried Pears', 30.00, '12 - 1 lb pkgs.'), (8, 'Northwoods Cranberry
Sauce', 40.00, '12 - 12 oz jars'), (9, 'Mishi Kobe Niku', 97.00, '18 - 500 g
pkgs.'), (10, 'Ikura', 31.00, '12 - 200 ml jars');
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> CREATE TABLE orders (id INT PRIMARY KEY, OrderDate DATETIME, OrderNumber
VARCHAR(50), CustomerId INT, TotalAmount DECIMAL(10,2) );
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO orderitem (Id, OrderId, ProductId, Quantity) VALUES (1, 1, 11,
12), (2, 1, 42, 10), (3, 1, 72, 5), (4, 2, 14, 9), (5, 2, 51, 40), (6, 3, 41, 10),
(7, 3, 51, 35), (8, 3, 65, 15);
Query OK, 8 rows affected (0.00 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql>
SOAL 1
SELECT
o.order_id,
c.name AS customer_name,
o.order_date,
o.total_amount
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
ORDER BY o.order_id;
SOAL 2
SELECT
o.order_id,
p.name AS product_name,
oi.quantity,
oi.price AS product_price,
(oi.quantity * oi.price) AS total_price
FROM order_items oi
JOIN orders o ON oi.order_id = o.order_id
JOIN products p ON oi.product_id = p.product_id
ORDER BY o.order_id, oi.order_item_id;
SOAL 3
SELECT
p.product_id,
p.name AS product_name,
SUM(oi.quantity) AS total_sold
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id
GROUP BY p.product_id, p.name
ORDER BY total_sold DESC;
SOAL 4
SELECT
o.order_id,
c.name AS customer_name,
o.order_date,
o.total_amount,
(o.total_amount / COUNT(oi.order_item_id)) AS avg_purchase_per_item
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
LEFT JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY o.order_id
ORDER BY o.order_date;
SOAL 5
SELECT
p.product_id,
p.name AS product_name,
YEAR(o.order_date) AS order_year,
MONTH(o.order_date) AS order_month
FROM products p
LEFT JOIN order_items oi ON p.product_id = oi.product_id
LEFT JOIN orders o ON oi.order_id = o.order_id
WHERE oi.product_id IS NULL
GROUP BY p.product_id, p.name, order_year, order_month
ORDER BY order_year, order_month, p.name;
--------------======
SELECT o.order_id, c.name AS customer_name, o.order_date, o.total_amount FROM
orders o JOIN customers c ON o.customer_id = c.customer_id ORDER BY o.order_id;