To Insert into table from another table we use insert statement with select statement.
Important Notes
To insert data a table from anathor table we must use insert statement with select statement.
At hhis queries, we don’t use values statement.
Insert into statement’s column count and select statement’s column count must be the same
Data types must be same at same order
The Syntax must be like below
1 2 |
Insert into TableName(columnNames) Select columnNames from TableName |
Query Examples
TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK
Example 1: Insert into the authors table, Random selected five students
1 2 |
Insert into authors(name,surname) Select top 5 name,surname from students order by newid() |
Example 2: Add the authors whose name contains “a” character from autors table to students table. The class of autors will have been ’12M’
1 2 |
Insert into students(name,surname,class) Select name,surname,'12M' from authors where name like '%a%' |
Example 3: Add students to the authors table whose class 11A
1 2 |
Insert into authors(name,surname) Select name,surname from students where class='11A' |