▶ Try SQL

SQL INSERT INTO

Add new rows to a database table.

Basic syntax — named columns

SQL
INSERT INTO customers (name, city, country) VALUES ('Priya Sharma', 'Bangalore', 'India');

Always list the column names explicitly. If you add or reorder columns later, the query still works correctly.

Insert without column list

You can omit column names if you supply values for every column in the exact table order:

SQL — risky, avoid in production
INSERT INTO customers VALUES (10, 'Priya Sharma', 'Bangalore', 'India');

Insert multiple rows at once

SQL
INSERT INTO customers (name, city, country) VALUES ('Alice', 'London', 'UK'), ('Bob', 'New York', 'USA'), ('Carlos','Madrid', 'Spain');

One INSERT with multiple value sets is much faster than three separate INSERTs — the database does one disk write instead of three.

INSERT ... SELECT — copy data from another table

SQL — archive old orders
INSERT INTO orders_archive (id, customer_id, amount, order_date) SELECT id, customer_id, amount, order_date FROM orders WHERE order_date < '2023-01-01';

Inserting NULL values

SQL
INSERT INTO customers (name, city, country, phone) VALUES ('Raman', 'Bangalore', 'India', NULL);

Omitting a nullable column from the list also inserts NULL automatically.

Handling duplicates — UPSERT

PostgreSQL — insert or update on conflict
INSERT INTO customers (id, name, city) VALUES (1, 'Raman Updated', 'Mumbai') ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, city = EXCLUDED.city;
Always use transactions for bulk inserts Wrap large INSERT ... SELECT or multi-row inserts in a transaction so that if anything fails, the partial insert rolls back completely.