Stop 09 · Modifying DataTopics · 2/9

INSERT INTO (VALUES and SELECT)

Part of the SQL Roadmap

In short

Every signup, order and payment starts life as an INSERT. Learn both ways to add rows, typed-in values and copied query results, and you can load anything into a table.

INSERT INTO adds new rows, either from literal values (one or many rows at once) or from the result of a SELECT. Always list the target columns so the statement does not break when the table changes.

The jaadu sits at a wooden office desk writing new entries into a huge open register, with a neat stack of paper forms on one side and a long printed roll on the other.

Think of it like this

The school admission register

The office clerk at a school in Indore adds new students to the register in two ways. Some are written in one by one from admission forms (VALUES). Others are copied in bulk from the entrance test results sheet, only for those who passed (SELECT). Same register, two ways in.

Watch it happen

Two ways to insert rows

INSERT ... VALUES

  • You type the data yourself
  • One row or many rows in one statement
  • Great for small fixes and test data

INSERT ... SELECT

  • Rows come from a query
  • Can filter, join and transform on the way
  • Great for copying, archiving and loading

Use VALUES when you know the data. Use SELECT when the data already lives in another table.

The big ideas

What you need to know

Always list the columns

INSERT INTO customers (name, city) keeps working even if someone adds a new column to the table later.

Many rows, one statement

Separate rows with commas after VALUES. It's faster and cleaner than ten separate inserts.

Order must line up

The first value goes into the first listed column, the second into the second, and so on.

Defaults fill the gaps

Columns you leave out get their DEFAULT, like an auto ID or NOW(), or NULL if none is set.

See it in code

Walk through the code

sql
1INSERT INTO customers (full_name, city, phone)2VALUES3  ('Aarav Gupta',  'Indore',    '9876500001'),4  ('Sneha Pillai', 'Kochi',     '9876500002'),5  ('Harpreet Kaur','Amritsar',  '9876500003');6 7INSERT INTO vip_customers (customer_id, full_name, city)8SELECT customer_id, full_name, city9FROM customers10WHERE total_spend >= 50000;

1/3Name the table and the exact columns you're filling.

A multi-row VALUES insert, then a bulk copy of high spenders. Both forms work the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

INSERT INTO customers VALUES (...) without a column list.

Do this instead

List the columns. Without them, the insert breaks or puts data in the wrong column as soon as the table changes.

Oops

Running an INSERT ... SELECT without checking what the SELECT returns.

Do this instead

Run the SELECT on its own first and look at the row count. Then add the INSERT line on top.

Your mission

Fill a table two ways

~20 min

Quick check

What happens to a column you leave out of the INSERT column list?

Go deeper

  1. 1Insert three rows with a single multi-row VALUES statement.
  2. 2Copy last month's orders into an archive table with INSERT INTO ... SELECT.
  3. 3Leave out auto-generated and defaulted columns and check the values the database fills in.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge