Beyond SELECT—learn how to change data with INSERT, UPDATE, and DELETE.
Once you understand how to query data with SELECT
, the next step is learning how to modify data in your database. That means inserting new records, updating existing ones, and deleting data when it’s no longer needed.
In this beginner-friendly guide, we’ll walk through how to use the three most essential SQL data manipulation statements: INSERT
, UPDATE
, and DELETE
. You’ll learn how to use them safely, with real-world examples and best practices.
Want to try these examples in your browser? Fire up the Galaxy SQL Editor to test queries with no setup.
The INSERT
statement adds new rows to a table.
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
INSERT INTO users (name, email)
VALUES ('Alice', 'alice@example.com');
This creates a new user with the name Alice and her email address.
You can insert multiple rows at once:
INSERT INTO users (name, email)
VALUES
('Bob', 'bob@example.com'),
('Charlie', 'charlie@example.com');
RETURNING *
(PostgreSQL) to confirm what was added.The UPDATE
statement changes values in one or more rows.
UPDATE table_name
SET column1 = value1
WHERE condition;
UPDATE users
SET email = 'alice.new@example.com'
WHERE name = 'Alice';
This updates Alice’s email address.
UPDATE users
SET name = 'Alicia', updated_at = NOW()
WHERE id = 5;
Always use a WHERE
clause to avoid updating every row!
-- Bad: Updates every user
UPDATE users SET is_active = false;
WHERE
to scope your updates.BEGIN
, COMMIT
) when changing multiple rows.The DELETE
statement removes rows from a table.
DELETE FROM table_name
WHERE condition;
DELETE FROM users WHERE id = 42;
This deletes the user with ID 42.
Like UPDATE
, DELETE
requires careful use of the WHERE
clause.
-- Warning: Deletes every row in the table
DELETE FROM users;
WHERE
clause.LIMIT
in some databases (e.g., MySQL) to delete in batches.When someone fills out a signup form, use INSERT
to create a new user record.
When a user edits their profile, use UPDATE
to save their changes.
When a user requests to delete their account, use DELETE
(or a soft delete via UPDATE
).
This is one of the most dangerous mistakes and can affect all rows in a table.
Ensure the number of columns matches the number of values:
-- Error: 2 columns but only 1 value
INSERT INTO users (name, email) VALUES ('Alice');
Check that you're inserting the correct data types (INTEGER
, TEXT
, BOOLEAN
, etc). Refer to our SQL Data Types guide for help.
Review conditions carefully before executing destructive queries.
UPDATE
or DELETE
queries with a SELECT
first:SELECT * FROM users WHERE id = 10;
BEGIN TRANSACTION
and ROLLBACK
to safely test changes.Being able to INSERT
, UPDATE
, and DELETE
data is foundational to managing databases. These statements give you full control over your records—but with great power comes great responsibility.
By following best practices and using safeguards, you’ll avoid data loss and make confident, precise changes.
Want to practice with real queries in a safe sandbox? Launch the Galaxy SQL Editor to try it yourself.
Continue learning: