Summary -
In this topic, we described about the INSERT Multiple Rows with detailed example.
We can INSERT multiple rows in a single INSERT statement at a time. we can insert multiple row values by grouping row values with open and close bracis () and separating each row with a comma.
Syntax -
INSER INTO table_name ( column1,column2, …, columnN)
VALUES (row1_value1, row1_value2, …, row1_valueN),
(row2_value1, row2_value2, …, row2_valueN),
…,
(rowM_value1, rowM_value2, …, rowM_valueN),
- table_name – Specifies the name of the table.
Example –
Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
Scenario – Insert multiple rows.
Requirement – Insert two rows of emp_id 005, 006 into employee_details table. The query was as follows –
INSERT INTO employee_details VALUES
(006,"Employee6","Analyst","Employee3","2019-10-08",18000.00,1000),
(007,"Employee7","Clerk","Employee3","2019-07-11",15000.00,2000);
By executing above query, we can insert two rows of emp_id 005, 006 on employee_details table at a time. The output was as follows –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
Scenario – Insert multiple rows with specifying columns.
Requirement – Insert two rows of emp_id 008, 009 with specified columns on employee_details table. The query was as follows –
INSERT INTO employee_details(emp_id, emp_name, designation)
VALUES (008, "Employee8", "Salesman"),
( 009, "Employee9", "Salesman");
By executing above query, we can insert two rows of emp_id 008, 009 with specified columns on employee_details table at a time. The updated table after insertion was as follows –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | ||||
009 | Employee9 | Salesman |