Difference between revisions of "INSERT .. SELECT"

From SQLZOO
Jump to: navigation, search
(Created page with " <h1>INSERT .. SELECT</h1> <table class='NoBorder'><td><tr> The table <code>games</code> shows the year and the city hosting the Olympic Games. </td><td> <table style='float:l...")
 
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
+
You can use the results of a SELECT statement to insert rows into another table.
<h1>INSERT .. SELECT</h1>
+
<source lang=sql>
<table class='NoBorder'><td><tr>
+
INSERT INTO games(yr,city)
 +
  SELECT yr+12, city FROM games;
 +
</source>
 
The table <code>games</code> shows the year and the city hosting the Olympic Games.
 
The table <code>games</code> shows the year and the city hosting the Olympic Games.
 +
<table><tr><td>
 +
<table class='Bordered'>
 +
<caption align='center'>'''games''' (before)</caption>
 +
<tr><th align='center'>'''yr'''</th><th align='center'>'''city'''</th></tr>
 +
<tr><td>2000</td><td align='left'>Sydney</td></tr>
 +
<tr><td>2004</td><td align='left'>Athens</td></tr>
 +
<tr><td>2008</td><td align='left'>Beijing</td></tr>
 +
</table>
 
</td><td>
 
</td><td>
<table style='float:left' class='Bordered'>
+
<table class='Bordered'>
<caption align='center'>'''games'''</caption>
+
<caption align='center'>'''games''' (after)</caption>
 
<tr><th align='center'>'''yr'''</th><th align='center'>'''city'''</th></tr>
 
<tr><th align='center'>'''yr'''</th><th align='center'>'''city'''</th></tr>
 
<tr><td>2000</td><td align='left'>Sydney</td></tr>
 
<tr><td>2000</td><td align='left'>Sydney</td></tr>
 
<tr><td>2004</td><td align='left'>Athens</td></tr>
 
<tr><td>2004</td><td align='left'>Athens</td></tr>
 
<tr><td>2008</td><td align='left'>Beijing</td></tr>
 
<tr><td>2008</td><td align='left'>Beijing</td></tr>
 +
<tr><td>2012</td><td align='left'>Sydney</td></tr>
 +
<tr><td>2016</td><td align='left'>Athens</td></tr>
 +
<tr><td>2020</td><td align='left'>Beijing</td></tr>
 
</table>
 
</table>
</table>
+
</td></tr></table>
+
 
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
Line 26: Line 39:
 
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'>
 
INSERT INTO games(yr,city)
 
INSERT INTO games(yr,city)
 
   SELECT yr+12, city FROM games;
 
   SELECT yr+12, city FROM games;
 +
 
SELECT * FROM games;
 
SELECT * FROM games;
 
</source>
 
</source>

Latest revision as of 21:46, 2 June 2016

You can use the results of a SELECT statement to insert rows into another table.

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

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

games (before)
yrcity
2000Sydney
2004Athens
2008Beijing
games (after)
yrcity
2000Sydney
2004Athens
2008Beijing
2012Sydney
2016Athens
2020Beijing
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