Insert into ... values (SELECT ... FROM ...)
To INSERT data from one table into another, use this trusty SQL pattern:
This is your ticket to easily transfer data from source_table
matching condition
to target_table
. Ensure the data types align like dancers in a perfect ballet, and no unique keys or constraints are running the stage.
For a quick example where an applicant gets the job:
Sudden existential crisis: what if you need a single-row insertion? No worry - put a single-row SELECT
statement on the stage with VALUES
:
Just ensure your subquery returns exactly one row - it's called a singular existential crisis for a reason!
Working with data and column targeting
Single-row insertions with unique values
Sometimes you like to spice things up a bit by mixing static values with a subquery:
Here, CURRENT_DATE
is an unchanging constant like a North Star, and the subquery is your journey through today's sales for the summary.
Column targeting and identifier quirks
In case you're a sharpshooter aiming for specific columns, list them after the INSERT INTO
clause:
Remember, each DBMS has its own unique dress code for identifiers - MS SQL Server loves square brackets [ ]
, Informix is a fan of the classic double quotes "
. For Informix users, JFYI, DELIMIDENT
is your go-to settings ring to twirl for delimited identifiers.
Schema twister
Table names are usually straightforward, but when referencing, understanding its schema or 'owner' (Informix's term of endearment) becomes important. Careful in ANSI mode; it could be a stickler for casing.
Aggregate functions - party tricks!
Aggregate functions in an INSERT
subquery are life of the party, making everyone raise their glasses to data summarization:
Inserting multiple constants and rows
Got a lot of things to say in one go? Add multiple constant values as a part of the SELECT
:
For multi-row inserts, just let INSERT INTO...
waltz with SELECT...
without needing the VALUES
chaperone:
Solving hiccups and caveats
Dealing with duplicates and conflicts
Running into mirrors everywhere with those duplicate or conflict errors? Use your DBMS-specific arsenal like ON CONFLICT
(PostgreSQL) or ON DUPLICATE KEY UPDATE
(MySQL).
Conditional romantic insertions
Like a selective cupid, you may want to insert rows only if they met a certain condition. Use the WHERE
clause in the SELECT
or sprinkle some CASE
statements within the SELECT
.
Taking the partition route
Partitioned tables? Make sure you whisper the partition's name in your INSERT INTO
clause, if your DBMS hearts it.
Locks and performance
Large data inserts are like a big party - locks and performance need to be managed. Think about fine-tuning transaction isolation levels or organizing small batch parties. SQL Server's BULK INSERT
can be a great DJ, especially for large data sets.
Was this article helpful?