Here’s a way to SELECT and insert INTO a new table.
Select and Insert into the syntax
In the INSERT use the INTO keyword and columns as needed.
INSERT INTO my_table1 (a_column1, b_column2, c_column3)
(
SELECT a_column1, b_column2, c_column3
FROM my_table2
WHERE condition
);
Sample SQL Query: Selecting data from one table, you can insert it into another table in a single query.
INSERT INTO promotion (deptnum, empname, yearsexp)
(SELECT deptno, name, exp
FROM employee
WHERE exp > 10) ;
Employee table
Three columns are present in the employee table. And three rows of data are present in it.
deptno | name | exp |
---|---|---|
A1 | Vishnu | 13 |
A2 | Ravi | 5 |
A3 | Mahi | 10 |
Promotion Table
Create Table promotion
(deptnum char(05),
empname char(20),
yearsexp number);
Here’s how to get rows from the employee table and insert them into the promotion table
The below query writes only one row to the promotion table.
INSERT INTO promotion (deptnum, empname, yearsexp)
(SELECT deptno, name, exp
FROM employee
WHERE exp > 10) ;
Related