The INSERT INTO Statement
The INSERT INTO statement adds new rows of data to a table.
💡 INSERT is how you get data INTO your database. Think of it as filling in a new row in a spreadsheet.
Basic Syntax
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
Insert a Single Row
INSERT INTO customers (first_name, last_name, email, city)
VALUES ('Alice', 'Johnson', 'alice@email.com', 'New York');
Insert Multiple Rows
INSERT INTO customers (first_name, last_name, email, city)
VALUES
('Bob', 'Smith', 'bob@email.com', 'Los Angeles'),
('Carol', 'Williams', 'carol@email.com', 'Chicago'),
('Dan', 'Brown', 'dan@email.com', 'Houston');
⚠️ When inserting multiple rows, if ANY row fails, ALL rows fail (in most databases). This ensures data consistency.
Insert Without Column Names
-- Must provide values for ALL columns in exact order
INSERT INTO customers
VALUES (1, 'Alice', 'Johnson', 'alice@email.com', 'New York', 'NY', '2024-01-15', 1);
INSERT INTO ... SELECT
Copy data from one table to another:
INSERT INTO archive_customers (first_name, last_name, email)
SELECT first_name, last_name, email
FROM customers
WHERE city = 'New York';
✏️ Exercise: Insert three new products into a 'products' table with columns: name, price, category, stock
See Solution
INSERT INTO products (name, price, category, stock)
VALUES
('Wireless Mouse', 29.99, 'Electronics', 150),
('Desk Lamp', 45.00, 'Office', 88),
('Notebook Pack', 12.50, 'Supplies', 300);
✅ Key Takeaways
- INSERT INTO adds new rows to a table
- Always specify column names for clarity and safety
- Insert multiple rows at once with comma-separated values
- INSERT INTO...SELECT copies data between tables
- Column count must match value count