Contents /
Previous /
Next
Inserting and Creating Tables Using INSERT/SELECT INTO
INSERT INTO
INSERT INTO allows
the output of a SELECT to be used to insert values into an existing table.
INSERT INTO table [ ( column [, ...] ) ] SELECT query
Example "List of student names":
INSERT INTO name_list (name) SELECT name FROM stud;
SELECT * from name_list ;
name
------
fred
tom
john
lisa
(4 rows)
SELECT INTO
SELECT INTO creates a new table and fills it with data computed by a
query. The data is not returned to the client, as it is with a normal
SELECT. The new table's columns have the names and data types
associated with the output columns of the SELECT.
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
* | expression [ AS output_name ] [, ...]
INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table
FROM from_table
Example "List of student names":
SELECT name AS stud_name INTO name_list FROM stud;
SELECT * from name_list ;
stud_name
-----------
fred
tom
john
lisa
(4 rows)