Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SQL
SQL
0/12 completed
Beginner
What is a database?SELECT: reading dataFiltering with WHERESorting and limiting results
Intermediate
Aggregates and GROUP BYJOINs: combining tablesINSERT, UPDATE and DELETECreating tables and constraints
Advanced
Subqueries and CTEsWindow functions
Pro
Indexes and transactionsDatabase design and normalization
Tracks / SQL / Intermediate

INSERT, UPDATE and DELETE

So far you've only read data. Now let's change it. These commands are powerful, and in production a careless UPDATE or DELETE can wipe out real data, so we'll also cover the habits pros use to stay safe.

INSERT: adding rows

SQL · editable

Always list the column names. It protects you if columns are added or reordered later.

Insert several rows at once

SQL · editable

We left out id. Because it's an INTEGER PRIMARY KEY, SQLite generates it automatically. (PostgreSQL uses SERIAL or GENERATED ... AS IDENTITY; MySQL uses AUTO_INCREMENT.)

UPDATE: changing rows

SQL · editable

⚠️ The most famous SQL mistake: forgetting WHERE. UPDATE products SET price = 0; sets every product to 0. Always write the WHERE clause first.

SQL · editable

DELETE: removing rows

SQL · editable

DELETE FROM orders; without WHERE empties the whole table.

Safe habits pros use

  1. SELECT first. Run the WHERE as a SELECT to see exactly which rows you'll touch, then change the command to UPDATE/DELETE.
  2. Use transactions so you can undo:
SQL · editable
  1. Soft deletes: many apps never truly delete. They set a deleted_at timestamp column and filter it out instead.
  2. Back up before bulk changes in production.

Upsert: insert or update

"Insert this row, or update it if it already exists" is so common that most databases support it:

SQL · editable

(PostgreSQL and SQLite use ON CONFLICT. MySQL uses ON DUPLICATE KEY UPDATE.)

Practice

  1. Add yourself as a new customer.
  2. Restock every product with stock = 0 to stock = 25.
  3. Delete orders with quantity greater than 5.
SQL · editable
✎ Check your understanding

1.What happens with DELETE FROM orders;?

2.What does ROLLBACK do?

← JOINs: combining tables