Difference between revisions of "INSERT .. SELECT"

From SQLZOO
Jump to: navigation, search
m (Reverted edits by Miacardenas (talk) to last revision by Connor)
Line 26: Line 26:
 
In this example you run the next three Olympic games in the same three venues:
 
In this example you run the next three Olympic games in the same three venues:
 
<source lang='sql' class='def e-oracle'>
 
<source lang='sql' class='def e-oracle'>
INSERT INTO scott.games(yr,city)
+
INSERT INTO games(yr,city)
   SELECT yr+12, city FROM scott.games;
+
   SELECT yr+12, city FROM games;
SELECT * FROM scott.games;
+
SELECT * FROM games;
 
</source>
 
</source>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>

Revision as of 10:24, 13 February 2014

INSERT .. SELECT

The table games shows the year and the city hosting the Olympic Games.

games
yrcity
2000Sydney
2004Athens
2008Beijing
schema:scott
 DROP TABLE games
 CREATE TABLE games(
  yr INTEGER,
  city VARCHAR(20));
INSERT INTO games VALUES (2000,'Sydney');
INSERT INTO games VALUES (2004,'Athens');
INSERT INTO games VALUES (2008,'Beijing');

The INSERT SELECT statement adds a new row to the table based on a SELECT statement: In this example you run the next three Olympic games in the same three venues:

INSERT INTO games(yr,city)
  SELECT yr+12, city FROM games;
SELECT * FROM games;
INSERT INTO games(yr,city)
  SELECT yr+12, city FROM games;
SELECT * FROM games;

See also

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense