Objectives:
- Learn the
INSERT INTO
statement syntax - Insert single rows and multiple rows into a table
- Understand and use the
INSERT INTO ... SELECT
statement
1. Introduction to INSERT INTO Statement (5 minutes)
Syntax Overview:
- The
INSERT INTO
statement is used to add new rows to a table.
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
INSERT INTO TestTable (ID, Name, DateOfBirth)
VALUES (1, 'John Doe', '1990-01-01');
2. Inserting a Single Row (10 minutes)
Basic Syntax:
- Insert a single row into a table.
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
VALUES (1, 'John', 'Doe', '2024-01-15');
Exercise:
- Write SQL queries to insert the following data into
Students
table:
(2, 'Jane', 'Smith', '2024-01-16')
(3, 'Emily', 'Johnson', '2024-01-17')
3. Inserting Multiple Rows (10 minutes)
Syntax for Multiple Rows:
- Insert multiple rows in a single statement.
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
VALUES
(4, 'Alice', 'Brown', '2024-01-18'),
(5, 'Bob', 'White', '2024-01-19');
Example:
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
VALUES
(6, 'Charlie', 'Davis', '2024-01-20'),
(7, 'Dana', 'Evans', '2024-01-21');
Exercise:
- Write SQL queries to insert the following data into
Students
table:
(8, 'Frank', 'Miller', '2024-01-22')
(9, 'Grace', 'Wilson', '2024-01-23')
(10, 'Hannah', 'Moore', '2024-01-24')
4. Using INSERT INTO … SELECT (10 minutes)
Syntax Overview:
- The
INSERT INTO ... SELECT
statement is used to insert data from one table into another.
INSERT INTO destination_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;
Example:
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
SELECT StudentID, FirstName, LastName, EnrollmentDate
FROM OldStudentsTable
WHERE EnrollmentDate > '2024-01-01';
Exercise:
- Write SQL queries to insert data from
OldStudentsTable
toStudents
table whereEnrollmentDate
is after ‘2024-01-01’.
5. Practical Exercise: Inserting Data (10 minutes)
- Task:
- Continue using the
Students
table. - Insert more sample data into
Students
table:
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
VALUES
(11, 'Ian', 'Walker', '2024-01-25'),
(12, 'Jack', 'Green', '2024-01-26');
- Exercises:
- Insert the following data into
Students
table:(13, 'Kelly', 'Lewis', '2024-01-27')
(14, 'Liam', 'Young', '2024-01-28')
(15, 'Mona', 'Hall', '2024-01-29')
- Use the
INSERT INTO ... SELECT
statement to copy rows fromOldStudentsTable
toStudents
whereEnrollmentDate
is before ‘2024-01-20’.
Exercises
- Inserting Single Rows:
- Exercise: Write SQL queries to insert the following rows into
Students
table:(1, 'John', 'Doe', '2024-01-15')
(2, 'Jane', 'Smith', '2024-01-16')
- Inserting Multiple Rows:
- Exercise: Insert the following data into
Students
table:(6, 'Charlie', 'Davis', '2024-01-20')
(7, 'Dana', 'Evans', '2024-01-21')
- Using INSERT INTO … SELECT:
- Exercise: Write a query to insert data from
OldStudentsTable
toStudents
whereEnrollmentDate
is after ‘2024-01-01’.
- Sample Insertion Tasks:
- Exercise: Execute the following tasks:
- Insert
(8, 'Frank', 'Miller', '2024-01-22')
,(9, 'Grace', 'Wilson', '2024-01-23')
, and(10, 'Hannah', 'Moore', '2024-01-24')
intoStudents
. - Insert data from
OldStudentsTable
toStudents
whereEnrollmentDate
is before ‘2024-01-20’.
- Insert
Homework:
- Practice Questions:
- Write SQL queries to:
- Insert multiple rows into
Students
table. - Copy rows from
OldStudentsTable
toStudents
whereEnrollmentDate
is between ‘2024-01-15’ and ‘2024-01-20’.
- Insert multiple rows into