☰ Learn SQL Tutorial Menu

Inserting, Updating, and Deleting Data (DML)

Written by Faisal Adnan · Updated 16 Sept 2026 · 3 min read


Now the other half of managing data: adding, changing, and removing rows.

INSERT — adding rows

-- One row, all columns in table order
INSERT INTO departments VALUES (7, 'Legal', 'Karachi');

-- One row, naming columns explicitly (recommended — safer if columns ever change order)
INSERT INTO departments (id, name, location) VALUES (7, 'Legal', 'Karachi');

-- Multiple rows in one statement
INSERT INTO departments (id, name, location) VALUES
  (8, 'Research', 'Islamabad'),
  (9, 'Procurement', 'Lahore');

INSERT from a SELECT

You can populate a table directly from a query — very useful for copying or archiving data:

CREATE TABLE lahore_departments AS
SELECT * FROM departments WHERE location = 'Lahore';

UPDATE — changing existing rows

UPDATE employees
SET salary = salary * 1.10
WHERE department_id = 1;

The most important rule of UPDATE: always include a WHERE clause. Without one, every single row in the table gets updated:

-- DANGER: this gives EVERY employee a raise, not just one department
UPDATE employees SET salary = salary * 1.10;

UPDATE with a subquery

UPDATE employees
SET salary = salary * 1.05
WHERE department_id = (SELECT id FROM departments WHERE name = 'Sales');

DELETE — removing rows

DELETE FROM orders WHERE status = 'cancelled';

Same rule applies: always use WHERE. DELETE FROM orders; with no WHERE removes every row in the table.

A safe habit: SELECT before you UPDATE/DELETE

Before running an UPDATE or DELETE, run the same condition as a SELECT first, to see exactly which rows will be affected:

-- Step 1: check what you're about to change
SELECT * FROM orders WHERE status = 'cancelled';

-- Step 2: once you're confident, run the real statement
DELETE FROM orders WHERE status = 'cancelled';

This habit alone will save you from the single most common — and most painful — SQL mistake.

Lesson 19 of 27

Sign in to track your progress and earn XP for every lesson you finish.

Practice tip: the built-in code runner is being upgraded. Until it is back, copy the examples into any SQL or Python tool you have (for example SQLite, PostgreSQL, MySQL, SQL Server or a free online editor) and run them there. You can keep reading and earning XP here.