SQL Loading Many Rows
What you will learn
- How to insert many rows in one statement
- How to use a staging table to check data before it goes in
- How
INSERT ... SELECTandCREATE TABLE AScopy data - How to generate rows and check that all arrived
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). The changes happen only in your browser's copy of the data; Reset data puts everything back. Run the cells from top to bottom.
1. Many rows in one statement
One INSERT with many value groups is much faster than many single inserts, and it is atomic: either all rows are added or none (lesson 5.5):
CREATE TABLE readings (id INTEGER PRIMARY KEY, sensor TEXT NOT NULL, value REAL NOT NULL, taken_on TEXT NOT NULL);
INSERT INTO readings (sensor, value, taken_on) VALUES
('s1', 20.5, '2026-10-01'), ('s1', 21.0, '2026-10-02'), ('s2', 18.2, '2026-10-01'),
('s2', 19.1, '2026-10-02'), ('s3', 25.4, '2026-10-01');
SELECT COUNT(*) AS rows_loaded FROM readings;
rows_loaded
-----------
5
(1 row)
CREATE TABLE readings (id INTEGER PRIMARY KEY, sensor TEXT NOT NULL, value REAL NOT NULL, taken_on TEXT NOT NULL);
INSERT INTO readings (sensor, value, taken_on) VALUES
('s1', 20.5, '2026-10-01'), ('s1', 21.0, '2026-10-02'), ('s2', 18.2, '2026-10-01'),
('s2', 19.1, '2026-10-02'), ('s3', 25.4, '2026-10-01');
SELECT COUNT(*) AS rows_loaded FROM readings;
rows_loaded
-----------
5
(1 row)
CREATE TABLE readings (id INTEGER PRIMARY KEY, sensor TEXT NOT NULL, value REAL NOT NULL, taken_on TEXT NOT NULL);
INSERT INTO readings (sensor, value, taken_on) VALUES
('s1', 20.5, '2026-10-01'), ('s1', 21.0, '2026-10-02'), ('s2', 18.2, '2026-10-01'),
('s2', 19.1, '2026-10-02'), ('s3', 25.4, '2026-10-01');
SELECT COUNT(*) AS rows_loaded FROM readings;
rows_loaded
-----------
5
(1 row)
If one row breaks a rule, the whole statement fails and nothing is added. That is usually what you want, but it means one bad row in a large file stops the load.
2. A staging table
For data that comes from outside (a file, a partner), do not insert it straight into the real table. Load it as it is into a staging table of plain text, check it, and only then copy the good rows (the same idea as ELT in Database Fundamentals, lesson 13.4):
CREATE TABLE staging_readings (sensor TEXT, value TEXT, taken_on TEXT);
INSERT INTO staging_readings VALUES
('s1', '22.0', '2026-10-03'), ('s2', 'n/a', '2026-10-03'), ('s3', '26.1', '2026-10-3'), ('', '24.0', '2026-10-03');
SELECT * FROM staging_readings;
sensor value taken_on
------ ----- ----------
s1 22.0 2026-10-03
s2 n/a 2026-10-03
s3 26.1 2026-10-3
24.0 2026-10-03
(4 rows)
CREATE TABLE staging_readings (sensor TEXT, value TEXT, taken_on TEXT);
INSERT INTO staging_readings VALUES
('s1', '22.0', '2026-10-03'), ('s2', 'n/a', '2026-10-03'), ('s3', '26.1', '2026-10-3'), ('', '24.0', '2026-10-03');
SELECT * FROM staging_readings;
sensor value taken_on
------ ----- ----------
s1 22.0 2026-10-03
s2 n/a 2026-10-03
s3 26.1 2026-10-3
24.0 2026-10-03
(4 rows)
CREATE TABLE staging_readings (sensor TEXT, value TEXT, taken_on TEXT);
INSERT INTO staging_readings VALUES
('s1', '22.0', '2026-10-03'), ('s2', 'n/a', '2026-10-03'), ('s3', '26.1', '2026-10-3'), ('', '24.0', '2026-10-03');
SELECT * FROM staging_readings;
sensor value taken_on
------ ----- ----------
s1 22.0 2026-10-03
s2 n/a 2026-10-03
s3 26.1 2026-10-3
24.0 2026-10-03
(4 rows)
Now find the problems with ordinary queries:
SELECT sensor, value, taken_on,
CASE WHEN sensor = '' THEN 'missing sensor'
WHEN value GLOB '*[^0-9.]*' THEN 'value is not a number'
WHEN taken_on NOT GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' THEN 'bad date'
END AS problem
FROM staging_readings;
sensor value taken_on problem
------ ----- ---------- ---------------------
s1 22.0 2026-10-03 NULL
s2 n/a 2026-10-03 value is not a number
s3 26.1 2026-10-3 bad date
24.0 2026-10-03 missing sensor
(4 rows)
SELECT sensor, value, taken_on,
CASE WHEN sensor = '' THEN 'missing sensor'
WHEN value GLOB '*[^0-9.]*' THEN 'value is not a number'
WHEN taken_on NOT GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' THEN 'bad date'
END AS problem
FROM staging_readings;
sensor value taken_on problem
------ ----- ---------- ---------------------
s1 22.0 2026-10-03 NULL
s2 n/a 2026-10-03 value is not a number
s3 26.1 2026-10-3 bad date
24.0 2026-10-03 missing sensor
(4 rows)
SELECT sensor, value, taken_on,
CASE WHEN sensor = '' THEN 'missing sensor'
WHEN value GLOB '*[^0-9.]*' THEN 'value is not a number'
WHEN taken_on NOT GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' THEN 'bad date'
END AS problem
FROM staging_readings;
sensor value taken_on problem
------ ----- ---------- ---------------------
s1 22.0 2026-10-03 NULL
s2 n/a 2026-10-03 value is not a number
s3 26.1 2026-10-3 bad date
24.0 2026-10-03 missing sensor
(4 rows)
Three of the four rows have a problem. Copy only the good rows:
INSERT INTO readings (sensor, value, taken_on)
SELECT sensor, CAST(value AS REAL), taken_on FROM staging_readings
WHERE sensor <> '' AND value NOT GLOB '*[^0-9.]*' AND taken_on GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]';
(1 row changed)
SELECT COUNT(*) AS readings_now FROM readings;
readings_now
------------
6
(1 row)
INSERT INTO readings (sensor, value, taken_on)
SELECT sensor, CAST(value AS REAL), taken_on FROM staging_readings
WHERE sensor <> '' AND value NOT GLOB '*[^0-9.]*' AND taken_on GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]';
(1 row changed)
SELECT COUNT(*) AS readings_now FROM readings;
readings_now
------------
6
(1 row)
INSERT INTO readings (sensor, value, taken_on)
SELECT sensor, CAST(value AS REAL), taken_on FROM staging_readings
WHERE sensor <> '' AND value NOT GLOB '*[^0-9.]*' AND taken_on GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]';
(1 row changed)
SELECT COUNT(*) AS readings_now FROM readings;
readings_now
------------
6
(1 row)
Keep the rejected rows too, with the reason, so that someone can fix them (Database Fundamentals, lesson 12.7).
3. INSERT ... SELECT and CREATE TABLE AS
INSERT INTO t SELECT ... copies query results into an existing table. CREATE TABLE t AS SELECT ... creates the table from the result in one step (the new table has no keys or rules, so add them if you need them):
CREATE TABLE country_summary AS
SELECT country, COUNT(*) AS customers FROM customers GROUP BY country;
SELECT country, customers FROM country_summary ORDER BY customers DESC, country LIMIT 3;
country customers
-------------- ---------
United Kingdom 35
Turkey 32
Brazil 30
(3 rows)
CREATE TABLE city_summary AS
SELECT city, COUNT(*) AS customers FROM customers GROUP BY city;
SELECT city, customers FROM city_summary ORDER BY customers DESC, city LIMIT 3;
city customers
----- ---------
Lima 25
Osaka 24
Hanoi 23
(3 rows)
CREATE TABLE year_summary AS
SELECT enrolled_year, COUNT(*) AS students FROM students GROUP BY enrolled_year;
SELECT enrolled_year, students FROM year_summary ORDER BY enrolled_year;
enrolled_year students
------------- --------
2023 83
2024 102
2025 86
2026 89
(4 rows)
4. Generating rows
A recursive query (chapter 7) can generate a series of rows, handy for test data or a calendar:
CREATE TABLE numbers (n INTEGER PRIMARY KEY);
INSERT INTO numbers (n)
WITH RECURSIVE count_up(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM count_up WHERE n < 10)
SELECT n FROM count_up;
SELECT COUNT(*) AS rows_generated, MIN(n) AS first, MAX(n) AS last FROM numbers;
rows_generated first last
-------------- ----- ----
10 1 10
(1 row)
CREATE TABLE numbers (n INTEGER PRIMARY KEY);
INSERT INTO numbers (n)
WITH RECURSIVE count_up(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM count_up WHERE n < 10)
SELECT n FROM count_up;
SELECT COUNT(*) AS rows_generated, MIN(n) AS first, MAX(n) AS last FROM numbers;
rows_generated first last
-------------- ----- ----
10 1 10
(1 row)
CREATE TABLE numbers (n INTEGER PRIMARY KEY);
INSERT INTO numbers (n)
WITH RECURSIVE count_up(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM count_up WHERE n < 10)
SELECT n FROM count_up;
SELECT COUNT(*) AS rows_generated, MIN(n) AS first, MAX(n) AS last FROM numbers;
rows_generated first last
-------------- ----- ----
10 1 10
(1 row)
5. Check that everything arrived
After a load, compare the numbers: rows in the source, rows loaded, rows rejected. They must add up:
SELECT (SELECT COUNT(*) FROM staging_readings) AS in_staging,
(SELECT COUNT(*) FROM readings WHERE taken_on = '2026-10-03') AS loaded_from_staging,
(SELECT COUNT(*) FROM staging_readings) - (SELECT COUNT(*) FROM readings WHERE taken_on = '2026-10-03') AS rejected;
in_staging loaded_from_staging rejected
---------- ------------------- --------
4 1 3
(1 row)
SELECT (SELECT COUNT(*) FROM staging_readings) AS in_staging,
(SELECT COUNT(*) FROM readings WHERE taken_on = '2026-10-03') AS loaded_from_staging,
(SELECT COUNT(*) FROM staging_readings) - (SELECT COUNT(*) FROM readings WHERE taken_on = '2026-10-03') AS rejected;
in_staging loaded_from_staging rejected
---------- ------------------- --------
4 1 3
(1 row)
SELECT (SELECT COUNT(*) FROM staging_readings) AS in_staging,
(SELECT COUNT(*) FROM readings WHERE taken_on = '2026-10-03') AS loaded_from_staging,
(SELECT COUNT(*) FROM staging_readings) - (SELECT COUNT(*) FROM readings WHERE taken_on = '2026-10-03') AS rejected;
in_staging loaded_from_staging rejected
---------- ------------------- --------
4 1 3
(1 row)
One staging row was good, so one row was loaded and three were rejected. A load that cannot account for every row is a load you cannot trust.
6. Speed: big loads
Some habits that make large loads faster (they are explained in Database Fundamentals, chapter 7):
- Use one transaction for the whole load, not one per row (lesson 5.5).
- Insert many rows per statement.
- Consider creating indexes after the load, not before.
Worked example: a safe bulk load
| Step | Do |
|---|---|
| 1 | load the file into a staging table, all columns text |
| 2 | check it with queries: missing values, bad numbers, bad dates, duplicates |
| 3 | BEGIN; |
| 4 | INSERT INTO real_table SELECT ... FROM staging WHERE <good rows> (or an upsert) |
| 5 | compare counts: staging = loaded + rejected |
| 6 | COMMIT; if the numbers add up, otherwise ROLLBACK; |
| 7 | keep the rejected rows with the reason |
The table names are made up.
Common mistakes
- Loading raw data straight into the real table.
- One transaction per row, which is slow.
- Not counting the rows in, loaded and rejected.
- Silently dropping bad rows.
- Forgetting that
CREATE TABLE AScopies no keys or constraints.
Key takeaways
- Insert many rows in one statement; the statement is all-or-nothing.
- Load outside data into a staging table, check it, then copy the good rows.
INSERT ... SELECTandCREATE TABLE AScopy query results into tables.- Always check that rows in = loaded + rejected.
Practice
1. How do you add three rows with one statement?
Show answer
List three value groups after VALUES, separated by commas.
2. Why load outside data into a staging table first?
Show answer
You can check and clean it with queries before it enters the real table, and nothing bad gets in.
3. What does CREATE TABLE t AS SELECT ... not copy?
Show answer
Keys, defaults and constraints: the new table has only the columns and the data.
4. What should rows in staging equal after a load?
Show answer
Rows loaded plus rows rejected.
5. Why use one transaction for a big load?
Show answer
It is much faster than saving each row separately, and you can roll everything back if the checks fail.
6. Name two checks to run on staging data.
Show answer
For example: missing values, values that are not numbers, badly formed dates, and duplicate keys.