INSERT INTO adds new rows to a table. You can give values for every column in order, or name only the columns you want to fill. Columns you leave out get their default value, which is NULL unless the table says otherwise. To add many rows in one go, put several value lists in the same statement, separated by commas.
INSERT INTO table_name VALUES (v1, v2, ...);
INSERT INTO table_name (col1, col2) VALUES (v1, v2), (v3, v4);
Table you need first
CREATE TABLE orderDetail (
id INT PRIMARY KEY,
product_name VARCHAR(50) NOT NULL,
order_date DATE,
price DECIMAL(10,2) NOT NULL,
quantity INT
);Run this once. If you did the previous set, you already have it.
Do these in order. Each question uses what the one before it created or changed.
How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.
Question 1Insert one record in the "orderDetail" table. Sample data: id 1, product_name Pen, order_date 2023-01-01, price 9.05, quantity 3.
Show the answer and the output
INSERT INTO orderDetail VALUES (1, 'Pen', '2023-01-01', 9.05, 3);Output
Query OK, 1 row affected.
Then check with SELECT * FROM orderDetail ORDER BY id
| id | product_name | order_date | price | quantity |
|---|---|---|---|---|
| 1 | Pen | 2023-01-01 | 9.05 | 3 |
Question 2Insert at least 5 records at once in the "orderDetail" table (your choice of data).
Show the answer and the output
INSERT INTO orderDetail VALUES
(2, 'Book', '2023-01-01', 520, 2),
(3, 'Ink', '2023-01-12', 50.55, 1),
(4, 'Map', '2023-01-19', 25, 5),
(5, 'Bottle', '2023-01-30', 600, 1),
(6, 'Marker', '2023-02-05', 80, 4);Output
Query OK, 5 rows affected.
Then check with SELECT * FROM orderDetail ORDER BY id
| id | product_name | order_date | price | quantity |
|---|---|---|---|---|
| 1 | Pen | 2023-01-01 | 9.05 | 3 |
| 2 | Book | 2023-01-01 | 520.00 | 2 |
| 3 | Ink | 2023-01-12 | 50.55 | 1 |
| 4 | Map | 2023-01-19 | 25.00 | 5 |
| 5 | Bottle | 2023-01-30 | 600.00 | 1 |
| 6 | Marker | 2023-02-05 | 80.00 | 4 |
Question 3Insert 2 records only into the id, product_name and price columns.
Show the answer and the output
INSERT INTO orderDetail (id, product_name, price) VALUES
(7, 'Pen', 65.90),
(8, 'Tape', 10);Output
Query OK, 2 rows affected.
Then check with SELECT * FROM orderDetail ORDER BY id
| id | product_name | order_date | price | quantity |
|---|---|---|---|---|
| 1 | Pen | 2023-01-01 | 9.05 | 3 |
| 2 | Book | 2023-01-01 | 520.00 | 2 |
| 3 | Ink | 2023-01-12 | 50.55 | 1 |
| 4 | Map | 2023-01-19 | 25.00 | 5 |
| 5 | Bottle | 2023-01-30 | 600.00 | 1 |
| 6 | Marker | 2023-02-05 | 80.00 | 4 |
| 7 | Pen | NULL | 65.90 | NULL |
| 8 | Tape | NULL | 10.00 | NULL |
order_date and quantity were not listed, so they are stored as NULL. This only works because those columns allow NULL.
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
