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
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 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
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework