SQL INSERT INTO

The INSERT INTO statement is used to insert new records into a table. You can specify which columns you want to insert data into and provide the corresponding values.

Syntax


-- Inserting data into specific columns
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

-- Inserting data into all columns
INSERT INTO table_name
VALUES (value1, value2, value3, ...);

        
  • table_name – The name of the table where the data will be inserted.
  • column1, column2, column3, ... – The columns where the data will be inserted (optional if inserting into all columns).
  • value1, value2, value3, ... – The values to be inserted into the specified columns.

Example


-- Insert a new employee record
INSERT INTO Employees (Name, Age, Position, Salary)
VALUES ('John Doe', 30, 'Developer', 50000);

        

In this example:

  • The query inserts a new record into the Employees table with values for Name, Age, Position, and Salary.

Notes

  • Data Types: Ensure that the data types of the values match the data types of the columns in the table.
  • Default Values: If you do not specify values for some columns, their default values (if any) will be used.
  • Inserting Multiple Rows: You can insert multiple rows in a single query by separating each set of values with commas.

Use Cases

  • Adding New Records: Use INSERT INTO to add new records to your database tables.
  • Batch Inserts: Insert multiple rows in one go to improve performance and reduce the number of queries executed.