Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

SQL INSERT INTO STATEMENT

INSERT INTO STATEMENT

The SQL INSERT statement is used to insert a single record or multiple records into a table.

While inserting a row, if the columns are not specified, it means that vales are added for all of the columns of the table resulting addition of a single row. If it is required to insert values for one or more specific column(s), then it is necessary to specify the name(s) of the column(s) in the SQL query.

The SQL SELECT statement can also be used to insert the rows of one table into another identical table.

While inserting the values, it is needed to enclose the values with single quotes for character or date values.

Syntax:

INSERT INTO < table name > (col1,col2,col3...col n)
VALUES (value1,value2,value3…value n);

Parameters:

Name Description
table_name Name of the table where data will be inserted.
col1,col2,col3,con n Column of the table.
value1,value2,value3,value n Values against each column.

You can use another syntax to insert data. Here, you don't specify column names of the associated table. So, value1 is inserted into the first column of a table, value2 into column2 and so on.

Syntax:

INSERT INTO < table name >
VALUES (value1,value2,value3…value n);

Parameters:

Name Description
table_name Name of the table where data will be inserted.
value1,value2,value3,value n Values against each column.

Syntax diagram - INSERT INTO STATEMENT

Syntax diagram - INSERT INTO STATEMENT

Example:

Sample table: agents


To add values'A001','Jodi','London','.12','075-1248798' for a single row into the table 'agents', the following SQL statement can be used :

INSERT INTO agents
VALUES ("A001","Jodi","London",.12,"075-1248798");

Pictorial Presentation:

SQL INSERT INTO STATEMENT

See our Model Database

SQL INSERT INTO Statement can be used in various forms. In the consequent pages, we have discussed those with the example, explanation, and pictorial presentations. Have a go through -

Insert null

You can use INSERT INTO statement to insert NULL Values.

Inserting the result of a query in another table

What if you want to insert data into a table while collecting data from another SQL query?

Insert using subqueries

You can use SUBQUERIES to INSERT data.

Insert using nested subqueries with any operator

You can even use NESTED SUBQUERIES with ANY OPERATOR to insert data.

Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.

Practice SQL Exercises

Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.

Previous: SELECT with DISTINCT Multiple Columns
Next: Insert null



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

Ref: https://bit.ly/3y6tpA3