EVIKA.ACADEMY›SQL Tutorial
SQL Tutorial›INSERT INTO

SQL INSERT INTO

Chapter 8 of 40 · Estimated read time: 7 min

The INSERT INTO statement adds new rows to a table. It comes in two forms: specifying column names explicitly (recommended), or providing values for every column in order (risky).

Syntax — Two Forms

-- Form 1: Name the columns (RECOMMENDED) INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3); -- Form 2: Values in column order (RISKY — breaks if schema changes) INSERT INTO table_name VALUES (value1, value2, value3, ...);
Always use Form 1 in real queries. If someone adds a column to the table later, Form 2 will fail or insert values into the wrong columns. Form 1 is explicit and safe.

Example 1 — Insert a Single Row

-- Add a new product
INSERT INTO products (product_id, product_name, category, price, stock, supplier_id)
VALUES (8, 'Mechanical Keyboard', 'Electronics', 2499, 45, 'S01');

-- Verify
SELECT * FROM products WHERE product_id = 8;
Result: A new row appears in products with product_id = 8.

Example 2 — Insert Multiple Rows at Once

-- Insert multiple customers in one statement
INSERT INTO customers (customer_id, customer_name, city, email, signup_date)
VALUES
  ('C06', 'Kavya Reddy',   'Chennai',    'kavya@example.com',  '2026-06-15'),
  ('C07', 'Nikhil Sharma', 'Pune',       'nikhil@example.com', '2026-07-20'),
  ('C08', 'Meera Iyer',    'Bengaluru',  'meera@example.com',  '2026-08-05');
Inserting multiple rows in a single INSERT is much faster than running three separate INSERT statements — the database commits all three rows in one transaction.

Example 3 — Insert Without Some Columns (NULL / Default)

If a column allows NULL or has a DEFAULT value, you can omit it from INSERT:

-- email is optional (allows NULL) — we omit it
INSERT INTO customers (customer_id, customer_name, city, signup_date)
VALUES ('C09', 'Dev Kumar', 'Noida', '2026-09-10');
-- email column will be NULL for this row
Omitting a required column (NOT NULL without a default) will cause an error. Always check the table schema before inserting.

Example 4 — INSERT INTO SELECT (Copy Data)

Copy rows from one table into another using INSERT INTO ... SELECT. Very useful for archiving, backups, or loading staging data:

-- Copy all Electronics products into a separate table
-- (assuming electronics_products table already exists)
INSERT INTO electronics_products (product_id, product_name, price, stock)
SELECT product_id, product_name, price, stock
FROM products
WHERE category = 'Electronics';
-- Create a backup table from a SELECT (PostgreSQL / BigQuery)
CREATE TABLE products_backup AS
SELECT * FROM products;

Example 5 — INSERT with Calculations

-- Insert a new order — calculate total_amount inline
INSERT INTO orders (order_id, customer_id, product_id, quantity, order_date, total_amount)
VALUES (
  1008,
  'C03',
  1,
  3,
  CURRENT_DATE,
  3 * (SELECT price FROM products WHERE product_id = 1)
);

Common INSERT Errors

❌ Duplicate key violation
Cause: Inserting a primary key value that already exists
Fix: Use INSERT OR IGNORE (SQLite/MySQL) or INSERT ... ON CONFLICT DO NOTHING (PostgreSQL)
❌ NULL constraint violation
Cause: Omitting a NOT NULL column with no default
Fix: Provide a value for all NOT NULL columns
❌ Data type mismatch
Cause: Inserting 'abc' into an INT column
Fix: Match the data type — quotes for text, no quotes for numbers
❌ Foreign key violation
Cause: customer_id does not exist in customers table
Fix: Insert parent record first, then the child record
✏️ Practice Exercises
1. Insert a new product: product_id=9, 'Standing Desk', Furniture, price=5999, stock=15, supplier_id='S03'.
2. Insert two new customers at once. Choose any values.
3. Using INSERT INTO SELECT, copy all orders with total_amount > 2000 into a table called large_orders.
Show Answers
-- 1.
INSERT INTO products (product_id, product_name, category, price, stock, supplier_id)
VALUES (9, 'Standing Desk', 'Furniture', 5999, 15, 'S03');

-- 2.
INSERT INTO customers (customer_id, customer_name, city, email, signup_date)
VALUES
  ('C10', 'Ravi Nair',   'Kochi',  'ravi@example.com',  '2026-10-01'),
  ('C11', 'Leela Rao',   'Mysuru', 'leela@example.com', '2026-10-15');

-- 3.
INSERT INTO large_orders (order_id, customer_id, product_id, quantity, order_date, total_amount)
SELECT order_id, customer_id, product_id, quantity, order_date, total_amount
FROM orders
WHERE total_amount > 2000;