SQL INSERT INTO Statement

The SQL INSERT INTO statement is used to insert one or multiple records to a database table.

SQL INSERT INTO Statement Syntax

INSERT INTO TableName (ColumnName1, ColumnName2, ColumnName3, ...)
VALUES (Value1, Value2, Value3, ...)

The column names can be ignored, in this case, the values will be inserted by the order of the columns when the table was created.

INSERT INTO TableName 
VALUES (Value1, Value2, Value3, ...)

We can insert records from other tables:

INSERT INTO Table1 (ColumnName1, ColumnName2, ColumnName3)
SELECT ColumnName1, ColumnName2, ColumnName3
FROM Table2
WHERE condition

Variations of inserting records from other tables:

INSERT INTO Table1 
SELECT ColumnName1, ColumnName2, ColumnName3
FROM Table2
WHERE condition
INSERT INTO Table1 
SELECT *
FROM Table2
WHERE condition

SQL INSERT INTO Statement Example

We start from an empty table Employees:

EmployeeId FirstName LastName Department Salary

We use the following statement to insert a new record into the table Employees:

INSERT INTO Employees (EmployeeId, FirstName, LastName, Department, Salary)
VALUES (203, 'Mazojys', 'Fxoj', 'Finance', 78000)

The table Employees will now look like:

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000

We may use shorthand to insert another new record:

INSERT INTO Employees
VALUES (204, 'Jozzh', 'Lnanyo', 'Finance', 45800)

The table Employees will now look like:

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800

If we don’t have complete information of all the columns to insert a new record:

INSERT INTO Employees
VALUES (205, 'Syllauu', 'Dfaafk')

The table Employees will now look like:

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk

To restore data from a backup table:

INSERT INTO Employees
SELECT *
FROM EmployeesBak
WHERE EmployeeId > 500

Leave a Comment