0% found this document useful (0 votes)
15 views8 pages

Quiz

The document outlines a series of MySQL commands executed in a terminal, including connecting to the MySQL server, creating databases and tables, inserting data, and querying information. It shows the creation of a database named 'npm_trans', along with tables for 'customer', 'products', 'orders', and 'orderitem', and demonstrates various SQL operations performed on these tables. The final commands display the data from the 'customer' and 'products' tables.

Uploaded by

govid.co.id
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)
15 views8 pages

Quiz

The document outlines a series of MySQL commands executed in a terminal, including connecting to the MySQL server, creating databases and tables, inserting data, and querying information. It shows the creation of a database named 'npm_trans', along with tables for 'customer', 'products', 'orders', and 'orderitem', and demonstrates various SQL operations performed on these tables. The final commands display the data from the 'customer' and 'products' tables.

Uploaded by

govid.co.id
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/ 8

D:\laragon\www

λ mysql -\u root -p


mysql: [ERROR] mysql: unknown option '-\'.

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

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| rental_mobil |
| app_penjualan |
| backup |
| buku_tamu |
| db_apotek |
| db_filo |
| db_masjid |
| db_pusakamart |
| db_toko |
| flaskmysql |
| information_schema |
| jadwal_sholat |
| laravel |
| mahasiswa_db |
| masjid_db |
| mysql |
| performance_schema |
| portal_berita |
| sessionmysql |
| spasgent_laravel11 |
| sys |
+--------------------+
21 rows in set (0.00 sec)

mysql> create database npm-trans;


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '-trans'
at line 1
mysql> create database npm_trans;
Query OK, 1 row affected (0.02 sec)

mysql> show databases


-> ;
+--------------------+
| Database |
+--------------------+
| rental_mobil |
| app_penjualan |
| backup |
| buku_tamu |
| db_apotek |
| db_filo |
| db_masjid |
| db_pusakamart |
| db_toko |
| flaskmysql |
| information_schema |
| jadwal_sholat |
| laravel |
| mahasiswa_db |
| masjid_db |
| mysql |
| npm_trans |
| performance_schema |
| portal_berita |
| sessionmysql |
| spasgent_laravel11 |
| sys |
+--------------------+
22 rows in set (0.00 sec)

mysql> use npm_trans;


Database changed
mysql> CREATE TABLE npm_trans (id INT PRIMARY KEY, FirstName VARCHAR(255), LastName
VARCHAR(255), City VARCHAR(100), Country VARCHAR(100), Phone VARCHAR(15) );
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;


+---------------------+
| Tables_in_npm_trans |
+---------------------+
| npm_trans |
+---------------------+
1 row in set (0.01 sec)

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> RENAME TABLE npm_trans TO customer;


Query OK, 0 rows affected (0.01 sec)

mysql> show tables;


+---------------------+
| Tables_in_npm_trans |
+---------------------+
| customer |
+---------------------+
1 row in set (0.00 sec)

mysql> describe customer;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| FirstName | varchar(255) | YES | | NULL | |
| LastName | varchar(255) | YES | | NULL | |
| City | varchar(100) | YES | | NULL | |
| Country | varchar(100) | YES | | NULL | |
| Phone | varchar(15) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> CREATE TABLE products ( id INT PRIMARY KEY, ProductName VARCHAR(255),


UnitPrice DECIMAL(10,2),Package VARCHAR(255) )
-> ;
Query OK, 0 rows affected (0.01 sec)

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> show tables;


+---------------------+
| Tables_in_npm_trans |
+---------------------+
| customer |
| products |
+---------------------+
2 rows in set (0.00 sec)

mysql> describe products;


+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| ProductName | varchar(255) | YES | | NULL | |
| UnitPrice | decimal(10,2) | YES | | NULL | |
| Package | varchar(255) | YES | | NULL | |
+-------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

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> show tables;


+---------------------+
| Tables_in_npm_trans |
+---------------------+
| customer |
| orders |
| products |
+---------------------+
3 rows in set (0.00 sec)

mysql> describe orders;


+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| OrderDate | datetime | YES | | NULL | |
| OrderNumber | varchar(50) | YES | | NULL | |
| CustomerId | int | YES | | NULL | |
| TotalAmount | decimal(10,2) | YES | | NULL | |
+-------------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> INSERT INTO orders (Id, OrderDate, OrderNumber, CustomerId, TotalAmount)


VALUES (1, '2012-07-04 00:00:00', 542378, 85, 440), (2, '2012-07-05 00:00:00',
542379, 79, 1863.4), (3, '2012-07-08 00:00:00', 542380, 34, 1813), (4, '2012-07-08
00:00:00', 542381, 84, 670.8), (5, '2012-07-09 00:00:00', 542382, 76, 3730), (6,
'2012-07-10 00:00:00', 542383, 34, 1444.8), (7, '2012-07-11 00:00:00', 542384, 14,
625.2), (8, '2012-07-12 00:00:00', 542385, 69, 2490.5), (9, '2012-07-15 00:00:00',
542386, 88, 517.8), (10, '2012-07-16 00:00:00', 542387, 35, 1119.9), (11, '2012-07-
17 00:00:00', 542388, 20, 2018.6);
Query OK, 11 rows affected (0.01 sec)
Records: 11 Duplicates: 0 Warnings: 0

mysql> CREATE TABLE orderitem ( Id INT PRIMARY KEY, OrderId INT,


ProductId INT, Quantity INT, FOREIGN KEY (OrderId) REFERENCES orders(Id) );
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;


+---------------------+
| Tables_in_npm_trans |
+---------------------+
| customer |
| orderitem |
| orders |
| products |
+---------------------+
4 rows in set (0.00 sec)

mysql> describe orderitem;


+-----------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------+------+-----+---------+-------+
| Id | int | NO | PRI | NULL | |
| OrderId | int | YES | MUL | NULL | |
| ProductId | int | YES | | NULL | |
| Quantity | int | YES | | NULL | |
+-----------+------+------+-----+---------+-------+
4 rows in set (0.00 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> SELECT * FROM orderitem;


+----+---------+-----------+----------+
| Id | OrderId | ProductId | Quantity |
+----+---------+-----------+----------+
| 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 |
+----+---------+-----------+----------+
8 rows in set (0.00 sec)

mysql> SELECT * FROM customer;


+----+------------+----------+------------+---------+----------------+
| id | FirstName | LastName | City | Country | Phone |
+----+------------+----------+------------+---------+----------------+
| 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 |
+----+------------+----------+------------+---------+----------------+
8 rows in set (0.00 sec)

mysql> SELECT * FROM products;


+----+---------------------------------+-----------+---------------------+
| id | ProductName | UnitPrice | Package |
+----+---------------------------------+-----------+---------------------+
| 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 |
+----+---------------------------------+-----------+---------------------+
10 rows in set (0.00 sec)

mysql> SELECT id, ProductName, FORMAT(UnitPrice, 0) AS UnitPrice, Package FROM


products;
+----+---------------------------------+-----------+---------------------+
| id | ProductName | UnitPrice | Package |
+----+---------------------------------+-----------+---------------------+
| 1 | Chai | 18 | 10 boxes x 20 bags |
| 2 | Chang | 19 | 24 - 12 oz bottles |
| 3 | Aniseed Syrup | 10 | 12 - 550 ml bottles |
| 4 | Chef Anton's Cajun Seasoning | 22 | 48 - 6 oz jars |
| 5 | Chef Anton's Gumbo Mix | 21 | 36 boxes |
| 6 | Grandma's Boysenberry Spread | 25 | 12 - 8 oz jars |
| 7 | Uncle Bob's Organic Dried Pears | 30 | 12 - 1 lb pkgs. |
| 8 | Northwoods Cranberry Sauce | 40 | 12 - 12 oz jars |
| 9 | Mishi Kobe Niku | 97 | 18 - 500 g pkgs. |
| 10 | Ikura | 31 | 12 - 200 ml jars |
+----+---------------------------------+-----------+---------------------+
10 rows in set (0.00 sec)

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;

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;

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;

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;

You might also like