✏️ Modifying DatabeginnerCommonly used

How to insert rows in SQL

Add one or more new rows to a table.

Table schema used in examplesA users table
CREATE TABLE users (
  id      SERIAL PRIMARY KEY,
  name    TEXT NOT NULL,
  email   TEXT UNIQUE NOT NULL,
  age     INTEGER,
  country TEXT
);
Insert a single rowAll databases
INSERT INTO users (name, email, age, country)
VALUES ('Alice', 'alice@example.com', 28, 'Kenya');

Inserts one row. Always specify column names — do not rely on column order.

When to use: Adding a single record.

Insert multiple rowsAll databases
INSERT INTO users (name, email, age, country)
VALUES
  ('Alice', 'alice@example.com', 28, 'Kenya'),
  ('Bob',   'bob@example.com',   32, 'Nigeria'),
  ('Carol', 'carol@example.com', 25, 'Ghana');

Insert multiple rows in one statement — much faster than separate INSERT statements.

When to use: Bulk inserting data.

Insert and return the new row (PostgreSQL)PostgreSQL
INSERT INTO users (name, email, age, country)
VALUES ('Alice', 'alice@example.com', 28, 'Kenya')
RETURNING id, name, email;

RETURNING gives you the inserted row back including generated values like id.

When to use: When you need the auto-generated ID or defaults after inserting.

Insert from a SELECTAll databases
INSERT INTO archived_users (name, email, country)
SELECT name, email, country
FROM users
WHERE active = false;

Inserts rows from a query result into another table.

When to use: Copying or archiving rows from one table to another.

Common Mistakes

  • Not specifying column names — your query breaks if columns are reordered or added
  • Inserting without handling UNIQUE constraint violations — use upsert instead
  • Inserting many rows with separate INSERT statements — use multi-row INSERT for performance
← Back to SQL Reference