Consider a university database. There are two tables namely Student and Marks.
In case, the marks of a few students has to be transferred from the marks table to the marks table, there are many ways to do so.
One can use subqueries (query nested in another query) but this is a complex and time consuming process.
Insert Into Select statement makes the job more easier. This statement is used to insert from the other tables.
insert [TOP(exp)[PERCENT]] into target_table column_list query
Table – Student
Table – Marks
If a user wants to transfer the marks to the Student table, the query is as follows –
insert into marks (roll number, name, gpa) select roll number, name, course from student
The values are inserted. To verify, the query is as follows –
select * from student
This is another way of inserting the values. By using TOP keyword, we can extract the desired values from the table –
insert TOP (6) PERCENT insert into marks(roll number, name, gpa) select roll number, name, course from student order by roll number
There is a slight difference in the query, yet we get the same result set. TOP is optional and it can be used when a user wants to extract only a particular number of rows from table.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Select statement in MS SQL Server
- Insert statement in MS SQL Server
- SELECT INTO statement in SQL
- SQL | INSERT INTO Statement
- Select into and temporary tables in MS SQL Server
- SQL | INSERT IGNORE Statement
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- Delete statement in MS SQL Server
- Reverse Statement Word by Word in SQL server
- Select top in MS SQL Server
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL | DELETE Statement
- SQL | UPDATE Statement
- SQL | Case Statement
- SQL | DESCRIBE Statement
- SQL | MERGE Statement
- MERGE Statement in SQL Explained
- CREATE and DROP INDEX Statement in SQL
- SQL | SELECT Query
- SQL | SELECT TOP Clause
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.