SQL INSERT INTO SELECT
INSERT INTO SELECT statement in SQL permits the insertion of records into a table. This is done based on the result of a SELECT query. This technique is powerful. It enables data copy from a table or source. Then this data is placed into a different table. It is possible to explain it easily using emojis. We can also use a simple example.
SQL INSERT INTO SELECT!
Suppose you are a master painter 🎨. Imagine you possess two gorgeous palettes. One of them carries existing colors while the other is blank. You desire to emulate some colors from the first palette to the second. Let's study a simple process.
Basic Syntax - Copying Colors 🖌️:
The SQL INSERT INTO SELECT statement works like this. It supports the replication of colors. This color replication is like copying data from one palette. It pastes them into another palette. The palette may be equated with a table.
INSERT INTO new_palette (color)
SELECT color
FROM old_palette
WHERE shade = 'light';
In this case, colors are to be copied. Position is from the old_palette. It goes to the new_palette. The copied colors should bear the shade 'light'.
Copying Multiple Properties - Multi-Colored Palette 🌈:
Copying multiple properties across tables is possible. We are creating a masterpiece of multiple colors.
INSERT INTO new_palette (color, intensity)
SELECT color, intensity
FROM old_palette
WHERE shade = 'bright';
Copying all content - Total Artwork 🖼️:
A situation exists when you aim to copy everything. Like creating a complete artwork. All properties are transferred for this!
INSERT INTO new_palette
SELECT *
FROM old_palette;
In this scenario, we copy all colors. Property attributes are copied too. It is from the old_palette to new_palette.
Just like a painter transfers colors to make a new masterpiece, INSERT INTO SELECT lets you transfer data. It moves from one table to another. In this way, you create your own SQL masterpiece! 🎨🌟 Happy SQL painting!
Follow us