Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 150

INSERT INTO SELECT in MySQL

In this lesson: Copy rows from one table to another in MySQL with INSERT INTO … SELECT — all/selected columns with output. In Hindi & English.

Copying rows between tables

Often you need to copy rows from one table into another — archiving old data, moving records, or building a summary table. MySQL's INSERT INTO ... SELECT does this in a single statement, with the full power of SELECT to filter and transform.

💡 INSERT INTO target SELECT ... FROM source — copy rows in one query, filtering as you go.

INSERT INTO ... SELECT

INSERT INTO archive (id, name)
SELECT id, name FROM users;
Query OK, 5 rows affected (0.01 sec)

The SELECT reads rows from users, and INSERT puts them into archive. No values are typed by hand — the data comes straight from the source table.

Copying only some rows

INSERT INTO archive (id, name)
SELECT id, name FROM users
WHERE active = 0;

Add a WHERE to the SELECT to copy only matching rows. Here only inactive users are archived. Any filter you can write in a SELECT works here.

Copying specific columns

INSERT INTO summary (customer, total)
SELECT customer_id, SUM(amount)
FROM orders
GROUP BY customer_id;

The SELECT can pick specific columns and even aggregate. Here we build a summary table of total spending per customer. The selected columns must match the target columns in order and type.

Transforming while copying

INSERT INTO products_new (id, name, price)
SELECT id, UPPER(name), price * 1.1
FROM products;

Because it is a full SELECT, you can transform data as you copy — here names become uppercase and prices rise by 10%. This is handy for migrations and cleanups.

Creating a table from a query

CREATE TABLE archive AS
SELECT * FROM users WHERE active = 0;

CREATE TABLE ... AS SELECT makes a new table and fills it from the query in one step. Note it copies the data and column types, but not primary keys, indexes or constraints — add those afterwards if needed.

Common mistakes

  • Mismatched columns. Selected columns must match the target list in order and type.
  • Forgetting WHERE. Without it, all rows are copied.
  • Expecting keys from CREATE TABLE AS. It copies data, not keys or indexes.
  • Duplicate-key errors. Copying into a table with existing keys can clash — use IGNORE or filter.

Summary

  • INSERT INTO ... SELECT copies rows from one table to another.
  • Add WHERE to copy only matching rows.
  • Select specific columns or aggregates; they must match the target.
  • Transform data with expressions while copying.
  • CREATE TABLE ... AS SELECT builds and fills a new table (no keys).

Next, add many rows with inserting multiple rows, or summarise with GROUP BY and HAVING.

Frequently Asked Questions

How do I copy rows from one table to another in MySQL?
Use INSERT INTO ... SELECT: INSERT INTO archive (id, name) SELECT id, name FROM users WHERE active = 0;. It reads rows from the SELECT and inserts them into the target table in a single statement.
How do I copy only some rows to another table?
Add a WHERE clause to the SELECT: only the matching rows are copied. For example, copying only inactive users, or only orders from last year, by filtering in the SELECT part.
Can I copy specific columns only?
Yes. List the target columns and select matching columns: INSERT INTO t2 (a, b) SELECT x, y FROM t1;. The number and types of selected columns must line up with the target columns.
How do I create a new table from a query?
Use CREATE TABLE ... AS SELECT: CREATE TABLE archive AS SELECT * FROM users WHERE active = 0;. This makes a new table and fills it with the query results in one step, though it does not copy keys and indexes.
Can I transform data while copying it?
Yes. The SELECT can include expressions, so you can change values as you copy — for example, SELECT id, UPPER(name), price * 1.1 FROM products copies names in uppercase and prices raised by 10%.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicMySQL Practical File Programs (Class 12 IT 802) Your progress: 150 / 151 (99%) · Lesson 151 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free