INSERT
Objectives
By the end of this lesson, you should be able to:
- Insert a single row into a table
- List columns explicitly rather than relying on table order
- Explain what happens to columns left out of an
INSERT
💡 Why this matters: A table with no rows in it isn’t useful yet.
INSERTis how real data actually gets into a database, everything covered from Module 3 exists to give that data somewhere correct to land.
⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database, using the
employeestable from Module 3.
The INSERT Statement
INSERT INTO employees (first_name, last_name, email, department, salary)
VALUES ('Erin', 'Walsh', 'erin.walsh@example.com', 'Engineering', 78000);
id | first_name | last_name | department | salary
----+------------+-----------+-------------+---------
1 | Erin | Walsh | Engineering | 78000.00
INSERT INTO employees (columns...) names exactly which columns are being supplied, VALUES (...) supplies one value per column, in the same order. id wasn’t mentioned, SERIAL (from Module 3) filled it in automatically.
Why List Columns Explicitly
It’s technically possible to skip the column list and supply values for every column, in table order:
INSERT INTO employees VALUES (2, 'Jordan', 'Reyes', 'jordan.reyes@example.com', 'Sales', 62000, '2026-07-28', true);
This works, but it’s fragile, it silently breaks if the table’s column order ever changes (after an ALTER TABLE ADD COLUMN, for instance), and it’s hard to read without checking the table’s definition. Listing columns explicitly, the way every example in this course does, avoids both problems, and lets columns with a DEFAULT (like hire_date and is_active) be skipped entirely.
Columns Left Out
Any column not mentioned in the column list gets its DEFAULT value (Module 3), or NULL if it has none and allows it:
INSERT INTO employees (first_name, last_name, email, department, salary)
VALUES ('Maya', 'Kapoor', 'maya.kapoor@example.com', 'Engineering', 81000);
first_name | hire_date | is_active
------------+------------+-----------
Maya | 2026-07-28 | t
hire_date and is_active weren’t mentioned, both filled in from their DEFAULT, exactly as covered in Module 3.
Try It
- Insert a new employee into
employees, supplyingfirst_name,last_name,email,department, andsalary, and confirmhire_dateandis_activefilled in automatically. - Try inserting a row without mentioning
email(which isNOT NULL, from Module 3), and confirm you get an error. - Explain, in your own words, why listing columns explicitly is safer than relying on table column order.
Recap
INSERT INTO table (columns...) VALUES (values...)adds one row, matching each value to the column list by position.- Listing columns explicitly is safer than relying on table order, and lets columns with a
DEFAULTbe skipped. - Any column left out gets its
DEFAULTvalue, orNULLif it allows one and has no default.
Next lesson: inserting several rows at once, in a single statement.