SQL INSERT Multiple Rows
The INSERT statement can also be used to add multiple rows in a single query. This saves time and ensures that multiple entries can be added efficiently. Each row to be inserted is separated by a comma in the VALUES clause.
The INSERT INTO statement must include the column names that match the values being provided, ensuring data consistency.
Example
This example adds multiple rows to the Orders table in one statement.
Code Example
-- Insert multiple rows into the Orders table
INSERT INTO Orders (Product, Category, Price)
VALUES
('Headphones', 'Gadgets', 150.00),
('Camera', 'Electronics', 500.00);
-- View the updated table
SELECT * FROM Orders;
Output
Product | Category | Price |
---|---|---|
Laptop | Electronics | 1200.00 |
Phone | Electronics | 800.00 |
Tablet | Gadgets | 600.00 |
Smartwatch | Gadgets | 250.00 |
Headphones | Gadgets | 150.00 |
Camera | Electronics | 500.00 |
Explanation
- The query uses the INSERT INTO statement to specify the table and columns.
- The VALUES clause contains multiple sets of data, each representing a new row.
- Each row is added to the table sequentially, as shown in the output.
Inserting multiple rows in one query is particularly useful for batch operations and ensures data insertion is efficient.