← Back to SQL, page 23

sql-0448 · Easy fresher

An archive copy shows customers' names in the country column and the other way round. Find the bug in the INSERT ... SELECT.

INSERT ... SELECT matches columns by position, not by name. The target list named (name, country) but the SELECT produced country first, so values went into the wrong columns. List the same columns in the same order on both sides.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
CREATE TABLE customers_archive (id INTEGER PRIMARY KEY, name TEXT, country TEXT);
CREATE TABLE customers_archive2 (id INTEGER PRIMARY KEY, name TEXT, country TEXT);

The query as written:

sql
INSERT INTO customers_archive (id, name, country) SELECT id, country, name FROM customers;
SELECT * FROM customers_archive ORDER BY id;
idnamecountry
1SpainAna
2UAEBilal
3FranceChloe
4IndiaDev

Every row has the country where the name should be. Both columns are TEXT, so no type error warned about the mismatch; with different types, SQLite's flexible typing would often still accept the data.

A corrected version:

sql
INSERT INTO customers_archive2 (id, name, country) SELECT id, name, country FROM customers;
SELECT * FROM customers_archive2 ORDER BY id;
idnamecountry
1AnaSpain
2BilalUAE
3ChloeFrance
4DevIndia

With matching order, the copy is correct. Writing the column lists on both sides, one per line and aligned, makes such swaps easy to spot in review, and a quick check such as comparing a few rows between source and target after each load catches them early.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
CREATE TABLE customers_archive (id INTEGER PRIMARY KEY, name TEXT, country TEXT);
CREATE TABLE customers_archive2 (id INTEGER PRIMARY KEY, name TEXT, country TEXT);

INSERT INTO customers_archive (id, name, country) SELECT id, country, name FROM customers;
SELECT * FROM customers_archive ORDER BY id;

INSERT INTO customers_archive2 (id, name, country) SELECT id, name, country FROM customers;
SELECT * FROM customers_archive2 ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • INSERT ... SELECT maps by position
  • Always list target columns
  • Match the select order exactly
  • Same types hide swaps

Common mistakes

  • Relying on names to match automatically
  • INSERT INTO t SELECT * after schema changes

Follow-ups

  • Why is INSERT INTO t SELECT * risky?
  • How would you verify an archive copy?

Tips

  • Line up both column lists