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
Some points to be noted for the SQL INSERT INTO statement:
Example:
Sample table: agents
+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+
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");
Visual Presentation:
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.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: SELECT with DISTINCT Multiple Columns
Next: Insert null
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql/insert-statement/sql-insert-command.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics