sqlcmd.net validated sql reference
beginner inserting MySQL MariaDB SQL Server PostgreSQL SQLite

Insert Rows From Another Table

Populate a table by selecting rows from another table using `INSERT INTO ... SELECT`.

Docker-validated Not currently validation-green

Copy only recently active users into a separate table

INSERT INTO recent_users (id, name) names the destination columns. The SELECT clause filters to users with a last_active date in 2025 or later, which excludes Alice. The column list in the INSERT clause must match the SELECT list in both count and compatible types. The final SELECT confirms that Bob and Carol were copied.

Rows loaded before the example query runs.
Setup
CREATE TABLE users (id INT, name VARCHAR(50), last_active DATE);

CREATE TABLE recent_users (id INT, name VARCHAR(50));

INSERT INTO
  users
VALUES
  (1, 'Alice', '2024-01-15'),
  (2, 'Bob', '2025-11-20'),
  (3, 'Carol', '2025-12-01');
Shared across supported engines.
SQL
INSERT INTO
  recent_users (id, name)
SELECT
  id,
  name
FROM
  users
WHERE
  last_active >= '2025-01-01';

SELECT
  id,
  name
FROM
  recent_users
ORDER BY
  id;
Returned rows for the shared example.
idname
2Bob
3Carol

The INSERT INTO ... SELECT syntax is identical across all supported engines.

Where this command helps.

  • copying a filtered subset of rows from one table into another
  • backfilling a new table from an existing one during a schema migration

What the command is doing.

INSERT INTO ... SELECT reads rows from any query and writes them directly into a target table without an intermediate step. The selected columns must match the target column list in order and type. The source can be a simple table scan, a join, a CTE, or any other valid query — making this the primary tool for backfilling data, archiving rows, copying filtered subsets, and loading staging tables.