Learn about SQL INSERT INTO Statement with the help of examples. Learn how to add new rows of data to a SQL table with the SQL INSERT INTO statement
In SQL, the INSERT INTO
statement is used to insert new rows in a database table.
-- insert a row in the Customers table
INSERT INTO Customers(customer_id, first_name, last_name, age, country)
VALUES
(7, 'Ron', 'Weasley', 31, 'UK');
Here, the SQL command inserts a new row into the Customers table with the given values.
The syntax of the SQL INSERT INTO
statement is:
INSERT INTO table_name(column1, column2, column3, ...)
VALUES
(value11, value12, value13, ...)
(value21, value22, value23, ...)
... ;
Here,
table_name
is the name of the table into which the rows are to be insertedcolumn1, column2, column3, ...
are the names of columns where the values are to be insertedvalue11, value12, value13, ...
, value21, value22, value23, ...
are the values to be insertedIn SQL, the INSERT INTO
statement is used to insert new row(s) into a database table.
For example,
-- insert a row in the Customers table
INSERT INTO Customers(customer_id, first_name, last_name, age, country)
VALUES
(5, 'Harry', 'Potter', 31, 'USA');
Here, the SQL command inserts a new row into the Customers table with the given values.
Example: SQL Insert Into
In a database table, the ID field is usually unique and auto incremented. In such cases, we can omit the value for that column during row insertion. For example,
-- insert a row to Customers table
-- skip auto incremented customer_id column
INSERT INTO Customers(first_name, last_name, age, country)
VALUES
('James', 'Bond', 48, 'USA');
Here, the SQL command automatically sets the new customer_id for the new row and inserts it in a table.
Example: SQL INSERT INTO
It's also possible to insert multiple rows to a database table at once. For example,
INSERT INTO Customers(first_name, last_name, age, country)
VALUES
('Harry', 'Potter', 31, 'USA'),
('Chris', 'Hemsworth', 43, 'USA'),
('Tom', 'Holland', 26, 'UK');
Here, the SQL command inserts three rows to the Customers table.
#sql