Updated June 8, 2023
Definition of SQL INSERT INTO
SQL insert into statement is used to insert data into the table which was we have specified in our query. We can insert data into the table by using two different ways. We can insert data into the table with specifying column name at the time of inserting values into the table. Also we can insert the data into the table without specifying column name at the time inserting values.
What is SQL Insert Into?
- This statement is very useful and important in any RDBMS database systems to insert records into the table.
- Insert into is the DML command used in SQL language to insert data into the table. We can insert data into the table by using multiple ways.
- We can insert single as well as multiple records in single query by using insert into statements. We can also insert data into the specified column which was we have mentioned in our query.
- We can also insert specified records from another table, also we can insert all the records from another table.
SQL INSERT INTO Statement
- Basically, SQL insert into statement is the SQL query which was used to insert records into the specified table. There are different ways to insert records into the table.
- Below syntax shows how we can insert records into the table by using SQL insert into statement.
Syntax –
- Insert into name_of_table values (val1, val2, …, valN);
- Insert into name_of_table (name_of_column1, name_of_column2, …, name_of_columnN) select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table.
- Insert into name_of_table (name_of_column1, name_of_column2, …, name_of_columnN) select name_of_column1, name_of_column2, …, name_of_columnN from name_of_table where condition.
- Insert into name_of_table (name_of_column1, name_of_column2, …, name_of_columnN) values (val1, val2, …, valN);
- Below is the description syntax of SQL insert into statement are as follows.
- Name of column – This is the table column which was used with insert into statement to insert data into table. We can also use insert into statement on specified column on which we need to insert a data.
- Name of table – This is the name of table on which we have inserting the records. We need to define table name while using sql insert statement.
- Insert into – This is the SQL command which was used to insert records into the table. We can insert records into the table as per condition which was we have used in our insert into statement.
- Where condition – The where condition in insert into statement of SQL will used to select specified row from table which was we have defined in where condition, after selecting those rows we have adding the same in our table. Where condition is very useful to retrieve specific row in SQL.
- Val – This is the values of column which was we have inserting into the table by using insert into statement. We need to use value as per datatype which was we have used at the time of creating a table.
- Select – This is the SQL statement which was used to select the specified data from table. In insert into statement we have using select statement to select the data from source table after selecting data from source table we have adding the same in target table by using insert into statement.
Below example shows that sql insert into statement is not case sensitive. In first example we have used keyword in uppercase letter while in second example we have used keyword in lowercase letters in both time it will inserting records into the table without returning any error.
insert into sql_insert values (101, 'ABC', '2020-03-04', 1, 'Mumbai',
INSERT INTO sql_insert values (102, 'ABC', '2020-03-04', 1, 'Mumbai', '1234567890');
SQL INSERT INTO Example
- To define the example of SQL insert into statement we are using table name as sql_insert. Below is the description of sql_insert table.
- In below example we have not defining column name at the time of adding values into the table. It is very simplest way to add the records into the table.
insert into sql_insert values (1001, 'ABC', '2018-04-04', 1, 'Mumbai', '1234567890');
select * from sql_insert;
2. In below example we have defining column name at the time of adding values into the table. In this example we can see that we have defined all the table columns at the time of inserting records into the table.
insert into sql_insert (id, stud_name, stud_birthdate, stud_grade, stud_addr, stud_phone) values (1001, 'ABC', '2018-04-04', 1, 'Mumbai', '1234567890');
select * from sql_insert;
3. In below example we are inserting multiple rows in single query. In below example we have inserting 3 rows in single query.
insert into sql_insert values (1003, 'ABC', '2018-04-04', 1, 'Mumbai', '1234567890'),
(1004, 'PQR', '2019-04-04', 1, ‘Pune’, '1234567892'),
(1005, 'XYZ', '2020-04-04', 1, 'Mumbai', '1234567893');
select * from sql_insert;
4. In below example we have inserting value into the specified column. We have inserting value into the id, stud_name, stud_birthdate and stud_phone column.
insert into sql_insert (id, stud_name, stud_birthdate, stud_phone) values (1006, 'XYZ' , '2018-04-04', '1234567890');
select * from sql_insert;
5. Below example shows that we have enabling the identity_insert property before inserting the insert into statement.
SET IDENTITY_INSERT sql_insert ON
insert into sql_insert (id, stud_name, stud_birthdate, stud_phone) values (1006, 'XYZ', '2018-04-04', '1234567890')
SET IDENTITY_INSERT sql_insert OFF
6. In below example we have inserting data from another table. We have inserting records into the specified columns. We are using ID, stud_name, stud_addr and stud_phone column to insert the records.
insert into sql_insert1 (ID, stud_name, stud_addr, stud_phone) select ID, stud_name, stud_addr, stud_phone from sql_insert;
select * from sql_insert1;
7. In below example we have inserting data from another table. We have inserting all records into the specified table.
Insert into sql_insert1 (ID, stud_name, stud_birthdate, stud_grade, stud_addr, stud_phone) select * from sql_insert;
Select * from sql_insert1;
Conclusion
Insert into is the DML command used in SQL language to insert data into the table. We can insert data into the table by using multiple ways. SQL insert into statement is used to insert data into the table which was we have specified in our query.
Recommended Article
This is a guide to SQL INSERT INTO. Here we discuss definition, What is SQL Insert Into, statement, examples along with code implementation and output. You may also have a look at the following articles to learn more –