Objectives:
- Learn the
INSERT INTOstatement syntax - Insert single rows and multiple rows into a table
- Understand and use the
INSERT INTO ... SELECTstatement
1. Introduction to INSERT INTO Statement (5 minutes)
Syntax Overview:
- The
INSERT INTOstatement 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
Studentstable:
(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
Studentstable:
(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 ... SELECTstatement 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
OldStudentsTabletoStudentstable whereEnrollmentDateis after ‘2024-01-01’.
5. Practical Exercise: Inserting Data (10 minutes)
- Task:
- Continue using the
Studentstable. - Insert more sample data into
Studentstable:
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
Studentstable:(13, 'Kelly', 'Lewis', '2024-01-27')(14, 'Liam', 'Young', '2024-01-28')(15, 'Mona', 'Hall', '2024-01-29')
- Use the
INSERT INTO ... SELECTstatement to copy rows fromOldStudentsTabletoStudentswhereEnrollmentDateis before ‘2024-01-20’.
Exercises
- Inserting Single Rows:
- Exercise: Write SQL queries to insert the following rows into
Studentstable:(1, 'John', 'Doe', '2024-01-15')(2, 'Jane', 'Smith', '2024-01-16')
- Inserting Multiple Rows:
- Exercise: Insert the following data into
Studentstable:(6, 'Charlie', 'Davis', '2024-01-20')(7, 'Dana', 'Evans', '2024-01-21')
- Using INSERT INTO … SELECT:
- Exercise: Write a query to insert data from
OldStudentsTabletoStudentswhereEnrollmentDateis 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
OldStudentsTabletoStudentswhereEnrollmentDateis before ‘2024-01-20’.
- Insert
Homework:
- Practice Questions:
- Write SQL queries to:
- Insert multiple rows into
Studentstable. - Copy rows from
OldStudentsTabletoStudentswhereEnrollmentDateis between ‘2024-01-15’ and ‘2024-01-20’.
- Insert multiple rows into
