Most SQL tutorials spend weeks on SELECT, and that makes sense, because reading data is what you’ll do most. But sooner or later you need to change data: add a new customer, fix a typo in a price, remove a test order. That’s the job of three commands: INSERT, UPDATE and DELETE.
They’re simple to learn. They’re also the commands that can do real damage if you’re careless, so along with the syntax I’ll share the habits that have saved me more than once.
Beginner · 10 min read · Works in MySQL, SQL Server and PostgreSQL
Quick answer:INSERT INTO products (product_id, name, price) VALUES (4, 'Stapler', 6.75) adds a new row.UPDATE products SET price = 0.90 WHERE product_id = 2 changes existing rows.DELETE FROM products WHERE product_id = 2 removes rows.
For UPDATE and DELETE, the WHERE clause decides which rows are touched. Leave it out and every row is changed.

Table of Contents
INSERT vs UPDATE vs DELETE: the difference
All three change data, but each one does a different job. The table shows what happens in each case, including the one that surprises beginners most: what happens when no row matches.
| Command | What it does | Uses WHERE? | When no row matches |
|---|---|---|---|
INSERT | Adds new rows | No | Always adds the row. Fails only if the key already exists (MySQL error 1062, SQL Server 2627). |
UPDATE | Changes values in rows that already exist | Yes | Changes nothing and gives no error: “0 rows affected”. |
DELETE | Removes existing rows | Yes | Removes nothing and gives no error. |
So UPDATE never creates a row. If you are not sure whether the row is there, check the number of rows affected, or use an upsert, which inserts or updates in one step.
The sample table
We’ll work with a small products table:
CREATE TABLE products (
product_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10,2) NOT NULL,
stock INT DEFAULT 0
);
| product_id | name | price | stock |
|---|---|---|---|
| 1 | Notebook | 2.50 | 120 |
| 2 | Pen | 0.80 | 300 |
| 3 | Backpack | 18.00 | 25 |
Want to follow along? Create the table above, then add the three starting rows. (It’s an INSERT, the first command of this guide, so you’re already practising.)
INSERT INTO products (product_id, name, price, stock) VALUES
(1, 'Notebook', 2.50, 120),
(2, 'Pen', 0.80, 300),
(3, 'Backpack', 18.00, 25);
Changing data is the one place where it pays to experiment on a copy. Use a practice database, not one your work depends on.
INSERT: adding new rows
INSERT INTO adds rows to a table. You list the columns, then the values in the same order:
INSERT INTO products (product_id, name, price, stock)
VALUES (4, 'Stapler', 6.75, 40);
The table now has four rows. A few things worth knowing:
- Text and dates go in single quotes. Numbers don’t.
- Always list the column names, even though SQL lets you skip them. If someone adds a column to the table later, an
INSERTwithout column names will break or, worse, put values in the wrong columns. - If you leave a column out, it gets its default value (here
stockwould be 0) orNULL.
Inserting several rows at once
Most databases let you add many rows in one statement, which is faster than running one INSERT per row:
INSERT INTO products (product_id, name, price, stock)
VALUES
(5, 'Ruler', 1.20, 80),
(6, 'Eraser', 0.50, 150),
(7, 'Marker', 1.90, 60);
Inserting from another table
You can also insert the result of a query. This is handy for copying or archiving data:
INSERT INTO products_archive (product_id, name, price)
SELECT product_id, name, price
FROM products
WHERE stock = 0;
Letting the database pick the id
In real systems you rarely type the id yourself. The column is set to generate it (AUTO_INCREMENT in MySQL, IDENTITY in SQL Server, GENERATED ALWAYS AS IDENTITY in PostgreSQL), and you just leave it out of the insert. There’s more on that in Primary Keys vs. Foreign Keys.
UPDATE: changing existing rows
UPDATE changes values in rows that already exist. You say which table, which columns get which new values, and which rows to touch:
UPDATE products
SET price = 2.75
WHERE product_id = 1;
Only the notebook changes. You can update several columns at once, and you can use the current value in the calculation:
-- 10% price increase and 50 more in stock for the backpack
UPDATE products
SET price = price * 1.10,
stock = stock + 50
WHERE product_id = 3;
The backpack is now 19.80 with 75 in stock.
Here is the table before and after those two updates (changed values in bold):
| product_id | name | price (before) | price (after) | stock (before) | stock (after) |
|---|---|---|---|---|---|
| 1 | Notebook | 2.50 | 2.75 | 120 | 120 |
| 2 | Pen | 0.80 | 0.80 | 300 | 300 |
| 3 | Backpack | 18.00 | 19.80 | 25 | 75 |
The mistake everyone makes once
Look at this:
UPDATE products
SET price = 0;
There’s no WHERE. SQL doesn’t ask “are you sure?”. It sets the price of every product to zero. I’ve seen this happen on a live system, and the restore took most of an afternoon. The WHERE clause is what limits the change, and it works exactly like it does in a SELECT (see WHERE, ORDER BY and LIMIT).
DELETE: removing rows
DELETE removes whole rows. Again, WHERE decides which ones:
DELETE FROM products
WHERE product_id = 2;
The pen is gone. And just like UPDATE, a DELETE without WHERE empties the whole table:
DELETE FROM products; -- removes every row!
DELETE vs TRUNCATE vs DROP
These three get mixed up a lot:
| Command | What it removes | Can use WHERE? | Notes |
|---|---|---|---|
DELETE | Some or all rows | Yes | Row by row, can be rolled back, fires triggers |
TRUNCATE TABLE | All rows | No | Much faster on big tables, resets identity counters in most databases |
DROP TABLE | The rows and the table itself | No | The table no longer exists afterwards |
Deleting rows that other tables depend on
If an orders table has a foreign key pointing at products, the database will usually refuse to delete a product that still has orders. That’s a good thing. It stops you creating orphan rows. Many systems don’t delete at all and use a “soft delete” instead, like an is_active column set to 0, so history is kept.
Insert or update? (upsert)
A very common job: “add this product, or update it if it’s already there”. Use INSERT for rows that don’t exist yet and UPDATE for rows that do. When you don’t know which, you need an upsert. Every database has its own syntax:
-- MySQL / MariaDB
INSERT INTO products (product_id, name, price, stock)
VALUES (2, 'Pen', 0.85, 50)
ON DUPLICATE KEY UPDATE price = VALUES(price),
stock = stock + VALUES(stock);
-- PostgreSQL
INSERT INTO products (product_id, name, price, stock)
VALUES (2, 'Pen', 0.85, 50)
ON CONFLICT (product_id)
DO UPDATE SET price = EXCLUDED.price,
stock = products.stock + EXCLUDED.stock;
-- SQL Server: update first, insert if nothing was updated
BEGIN TRANSACTION;
UPDATE products WITH (UPDLOCK, SERIALIZABLE)
SET price = 0.85, stock = stock + 50
WHERE product_id = 2;
IF @@ROWCOUNT = 0
INSERT INTO products (product_id, name, price, stock)
VALUES (2, 'Pen', 0.85, 50);
COMMIT;
The pen already existed, so its price became 0.85 and its stock went from 300 to 350. In MySQL 8.0.20 and later, VALUES() still works but is deprecated; the new form is INSERT ... VALUES (...) AS new ON DUPLICATE KEY UPDATE price = new.price. The SQL Server version uses lock hints so two users running it at the same moment can’t both insert. The full story, including MERGE and its traps, is in MERGE and Upsert in SQL Server and MySQL.
Watch out for REPLACE INTO
MySQL also has REPLACE INTO, which looks like an easy upsert. It isn’t: when the row exists, it deletes it and inserts a new one. Any column you didn’t list goes back to its default or NULL:
-- Row before: id = 1, a = 10, b = 20
REPLACE INTO t (id, a) VALUES (1, 99);
-- Row after: id = 1, a = 99, b = NULL (b is lost!)
Because it’s a delete, it also fires delete triggers and can trigger ON DELETE CASCADE on child tables. Prefer ON DUPLICATE KEY UPDATE.
INSERT, UPDATE and DELETE in real projects
The single-row examples above are where everyone starts. In business systems, most changes touch many rows at once, based on another table. These patterns come up all the time.
UPDATE using another table
A supplier sends a new price list. You load it into a table called price_updates, then update the products that appear in it. The syntax is different in each database:
-- SQL Server
UPDATE p
SET p.price = u.new_price
FROM products AS p
JOIN price_updates AS u ON u.product_id = p.product_id;
-- MySQL / MariaDB
UPDATE products p
JOIN price_updates u ON u.product_id = p.product_id
SET p.price = u.new_price;
-- PostgreSQL
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.product_id;
Before running it, turn it into a SELECT with the same JOIN and look at old and new prices side by side. If price_updates could contain the same product twice, fix that first: with duplicates, which price “wins” is not defined.
DELETE using another table
Delete products that are out of stock and have never been ordered. The “never been ordered” part needs the order lines table:
-- Works in SQL Server, MySQL and PostgreSQL
DELETE FROM products
WHERE stock = 0
AND NOT EXISTS (SELECT 1
FROM order_lines ol
WHERE ol.product_id = products.product_id);
NOT EXISTS is the safe choice here. NOT IN with a subquery returns nothing if the subquery contains a NULL, as explained in SQL LIKE, IN and BETWEEN.
Deleting millions of rows: work in batches
Clearing old log or history rows with one big DELETE can run for a long time, lock the table for everyone, and grow the transaction log enormously. Delete in small batches instead, so each batch is short:
-- SQL Server
DECLARE @rows INT = 1;
WHILE @rows > 0
BEGIN
DELETE TOP (5000) FROM audit_log
WHERE created_at < '2025-01-01';
SET @rows = @@ROWCOUNT;
END;
-- MySQL / MariaDB: repeat until 0 rows are affected
DELETE FROM audit_log
WHERE created_at < '2025-01-01'
LIMIT 5000;
An index on created_at makes each batch fast. Run the job outside busy hours. If you need to remove all rows and nothing references the table, TRUNCATE TABLE is faster still.
Keep a record of what changed
When a user changes a price or deletes a document, someone will eventually ask “who changed this, and what was it before?” SQL Server’s OUTPUT clause returns the old and new values from the same statement, and can write them straight into a history table:
UPDATE products
SET price = price * 1.10
OUTPUT deleted.product_id,
deleted.price AS old_price,
inserted.price AS new_price,
SYSDATETIME()
INTO price_history (product_id, old_price, new_price, changed_at)
WHERE product_id = 3;
In OUTPUT, deleted holds the values before the change and inserted the values after. PostgreSQL has RETURNING for the same purpose. In MySQL, use a trigger (see SQL Triggers Explained Simply) or write the history row in the same transaction.
Soft delete instead of DELETE
Many business systems never delete customers, products or documents. They mark them instead, so reports and old invoices still work:
UPDATE customers
SET is_active = 0,
deactivated_at = CURRENT_TIMESTAMP
WHERE customer_id = 42;
Lists and search screens then filter on is_active = 1. In SQL Server, a filtered index such as CREATE INDEX IX_customers_active_name ON customers (name) WHERE is_active = 1; keeps those searches fast.
Habits that keep your data safe
These have saved me from disaster more than once:
- Run a SELECT first. Write the
WHEREclause as aSELECT, check the rows it returns, then changeSELECT *toUPDATEorDELETE. - Use a transaction. Start with
BEGIN TRANSACTION, run your change, check the result, thenCOMMITorROLLBACK. I explain this properly in SQL Transactions and ACID Explained. - Filter on the primary key whenever you mean one specific row.
WHERE name = 'Pen'might match more rows than you expect. - Check the “rows affected” message. If you expected 1 and it says 4,000, something is wrong.
- Don’t experiment on production. Test on a copy first.
Practice: five changes to make
Start from a fresh products table with only the three starting rows (drop it and create it again if you’ve been experimenting). Do the five tasks in order, because each one builds on the last.
- Add a new product: id 8, “Glue Stick”, price 1.10, stock 70.
- The supplier raised the pen price. Make it 0.90.
- A delivery of 25 notebooks arrived. Add them to the stock without typing the new total yourself.
- Remove every product that costs less than 1.00. Check which rows will go before you delete them.
- Give a 10% discount on every product with more than 100 in stock.
Show the answers
-- 1.
INSERT INTO products (product_id, name, price, stock)
VALUES (8, 'Glue Stick', 1.10, 70);
-- 2.
UPDATE products
SET price = 0.90
WHERE product_id = 2;
-- 3. Let SQL do the sum: 120 + 25 = 145
UPDATE products
SET stock = stock + 25
WHERE product_id = 1;
-- 4. Look first, then delete with exactly the same WHERE
SELECT * FROM products WHERE price < 1.00; -- Pen 0.90
DELETE FROM products WHERE price < 1.00;
-- 5. Only the notebook (145 in stock) qualifies: 2.50 becomes 2.25
UPDATE products
SET price = price * 0.90
WHERE stock > 100;
If everything went right, SELECT * FROM products now shows three rows: Notebook 2.25 with 145 in stock, Backpack 18.00 with 25, and Glue Stick 1.10 with 70. If yours is different, go back step by step and check each WHERE. That habit of checking is the most valuable thing this exercise teaches.
Quick reference
| Task | Command |
|---|---|
| Add a row | INSERT INTO t (a, b) VALUES (1, 'x'); |
| Add many rows | INSERT INTO t (a, b) VALUES (1,'x'), (2,'y'); |
| Copy rows from a query | INSERT INTO t (a, b) SELECT a, b FROM s WHERE ...; |
| Change values | UPDATE t SET b = 'z' WHERE a = 1; |
| Remove rows | DELETE FROM t WHERE a = 1; |
| Remove all rows fast | TRUNCATE TABLE t; |
Frequently asked questions
What is the difference between INSERT and UPDATE?
INSERT adds new rows to a table. UPDATE changes values in rows that already exist and does nothing if no row matches its WHERE clause. If you’re not sure whether the row exists, use an upsert, shown above.
Does UPDATE add a row if it does not exist?
No. An UPDATE whose WHERE matches nothing finishes without an error and changes 0 rows. We tested it: UPDATE products SET price = 5.00 WHERE product_id = 99 reports “Rows matched: 0” in MySQL and UPDATE 0 in PostgreSQL, and the table still has 3 rows. To add the row when it is missing, use an upsert.
How do I change one row in SQL?
Use UPDATE with a WHERE on the primary key, so exactly one row can match: UPDATE products SET price = 2.75 WHERE product_id = 1;. People sometimes search for “alter a row”, but ALTER TABLE changes the table’s structure (its columns), not the data in a row.
Can I undo an UPDATE or DELETE?
Only if you ran it inside a transaction that you haven’t committed yet: ROLLBACK undoes it. Once it is committed (and most tools commit automatically), the only way back is a backup or a history table. That’s why the habits above matter.
How do I see how many rows were changed?
Tools like SSMS and MySQL Workbench show “rows affected” after each statement. In code, use @@ROWCOUNT in SQL Server, ROW_COUNT() in MySQL, or the value your database library returns (for example, Laravel’s update() returns the number of rows changed).
Summary
INSERTadds rows,UPDATEchanges existing rows,DELETEremoves rows.- For
UPDATEandDELETE, theWHEREclause decides which rows change. Without it, every row changes. - An
UPDATEthat matches nothing does not fail and does not insert. Use an upsert when you do not know whether the row exists. - Run a
SELECTwith the sameWHEREfirst, and use a transaction when the change matters.
If you’re still getting comfortable reading data, go back to SQL for Complete Beginners. Otherwise, the natural next step is learning how transactions protect your changes.
Learn SQL step by step
INSERT, UPDATE and DELETE are Chapter 17 of my book Simplify SQL for Absolute Beginners. It walks you through the whole path, from your first SELECT to transactions and window functions, with exercises and answers on the free ShopDB practice database.
As an Amazon Associate I earn from qualifying purchases.
Related articles
- SQL Constraints Explained Simply: NOT NULL, UNIQUE, CHECK and DEFAULT.
- SQL Triggers Explained Simply: run code automatically when data changes.
- SQL Injection Explained Simply: how attacks work and how to stop them.
- SQL Data Types Explained Simply: choosing the right type for each column.
- MERGE and Upsert in SQL Server and MySQL: Insert or Update Safely: insert a row or update it if it already exists, in one safe step.