Lessons › SQL › Chapter 7: Creating and changing tables
SQL lesson
INSERT INTO
INSERT INTO adds a row to a table. You say which table and which columns, then give the values in the same order. Text, dates and codes that mix letters and numbers, like 'O2002', go in single quotes. Plain numbers, like 5000, don't. To add several rows at once, put a comma between them: VALUES (...), (...).
Try this lesson free →Example
Add rows to a table
INSERT INTO targets (store, month, target)
VALUES ('Lagos', '2024-03-01', 5000)| store | month | target |
|---|---|---|
| Lagos | 2024-03-01 | 5000 |
Step by step
- Names the table, then the columns you're filling.
INSERT INTO targets (store, month, target) VALUES ('Lagos', '2024-03-01', 5000) - One value per column, in the same order. Text, dates and codes like 'R4' go in single quotes.
INSERT INTO targets (store, month, target) VALUES ('Lagos', '2024-03-01', 5000) - One bracket per row, with a comma between the rows.
INSERT INTO targets (store, month, target) VALUES ('Lagos', '2024-03-01', 5000)
Practice
1. Add the Lagos store's target to targets: store Lagos, month 2024-03-01, target 5000.
INSERT INTO targets (store, month, target) VALUES ('Lagos', '2024-03-01', 5000)2. Add two returns to returns in one statement: R4 for order O2002 with a refund of 49.50, and R5 for order O2004 with a refund of 149.50.
INSERT INTO returns (return_id, order_id, refund) VALUES ('R4', 'O2002', 49.50), ('R5', 'O2004', 149.50)3. Add a staff member to staff: staff_id S7, full_name Ada Obi, start_date 2024-04-01.
INSERT INTO staff (staff_id, full_name, start_date) VALUES ('S7', 'Ada Obi', '2024-04-01')4. Add a row to stock_counts: product_id P2003, store Lagos, units 40.
INSERT INTO stock_counts (product_id, store, units) VALUES ('P2003', 'Lagos', 40)5. Add two rows to targets in one statement: the Abuja store's target for 2024-03-01 is 4000, and the Kano store's is 2500.
INSERT INTO targets (store, month, target) VALUES ('Abuja', '2024-03-01', 4000), ('Kano', '2024-03-01', 2500)Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →