CodingNic

CRUD Operations

INSERT

CRUD Operations 10 min read

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. INSERT is 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 employees table from Module 3.

The INSERT Statement

sql
INSERT INTO employees (first_name, last_name, email, department, salary)
VALUES ('Erin', 'Walsh', 'erin.walsh@example.com', 'Engineering', 78000);
text
 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:

sql
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:

sql
INSERT INTO employees (first_name, last_name, email, department, salary)
VALUES ('Maya', 'Kapoor', 'maya.kapoor@example.com', 'Engineering', 81000);
text
 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

  1. Insert a new employee into employees, supplying first_name, last_name, email, department, and salary, and confirm hire_date and is_active filled in automatically.
  2. Try inserting a row without mentioning email (which is NOT NULL, from Module 3), and confirm you get an error.
  3. 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 DEFAULT be skipped.
  • Any column left out gets its DEFAULT value, or NULL if it allows one and has no default.

Next lesson: inserting several rows at once, in a single statement.