Data Manipulation

SQL INSERT

Inserting Data with INSERT INTO

INSERT INTO adds rows, supporting multi-row inserts for efficiency.

Understanding the SQL INSERT Statement

The INSERT INTO statement is a crucial part of SQL, used to add new rows of data to a table. This command allows you to insert a single row or multiple rows at once, enhancing efficiency when dealing with large datasets.

Basic Syntax of INSERT INTO

The basic syntax for the INSERT INTO statement is as follows:

Here, table_name is the name of the table where you want to insert data, and column1, column2, column3, ... are the names of the columns in the table. The VALUES keyword is followed by the values you want to insert into each column.

Inserting a Single Row

To insert a single row into a table, you specify the values for each column:

This SQL command inserts a new row into the employees table, specifying values for the first_name, last_name, and email columns.

Inserting Multiple Rows

SQL also allows for inserting multiple rows in a single query, which can significantly improve performance:

Here, three new rows are added to the employees table with a single INSERT INTO statement, making the operation more efficient.

Inserting Data from Another Table

It is also possible to insert data from one table into another. This is useful for copying data between tables:

This command copies all employees in the 'Sales' department from the employees table to the employees_backup table.

Conclusion

The INSERT INTO statement is a powerful SQL tool for adding data to tables, whether you're inserting a single row, multiple rows, or copying data from another table. Mastering its usage enhances your ability to manage databases efficiently.

Previous
ANY and ALL