Lesson 9: Inserting Data
Introduction
The INSERT INTO
statement is used to add new rows of data into a table. It’s essential for populating tables with information.
Insert a Single Row
Specify column names and provide values for those columns.
SQL
INSERT INTO customers (first_name, last_name, email, city)
VALUES ('Alice', 'Brown', 'alice.brown@email.com', 'Boston');
Insert Multiple Rows
Use a single INSERT
statement with multiple sets of values.
SQL
INSERT INTO customers (first_name, last_name, email, city)
VALUES
('Tom', 'Evans', 'tom.evans@email.com', 'Chicago'),
('Emma', 'Wilson', 'emma.wilson@email.com', 'Seattle');
Insert Data from Another Table
Copy data from one table into another using INSERT ... SELECT
.
SQL
INSERT INTO archived_customers (first_name, last_name, email)
SELECT first_name, last_name, email
FROM customers
WHERE city = 'Chicago';
Handling NULL and DEFAULT Values
If a column allows NULL
, you can skip it. If a column has a DEFAULT
value, it will be automatically applied if not provided.
SQL
INSERT INTO customers (first_name, last_name)
VALUES ('Sam', 'Taylor');
Practice Quiz
Test your knowledge of INSERT statements: