Back to all posts

SQL DELETE Statement Explained with Examples (2026)

For SQL beginners who can write a SELECT with WHERE and are about to remove rows from a table for the first time.

On this page

DELETE removes only the rows of a table that match a condition. For example, you can delete the cancelled orders from an orders table.

The basic DELETE has this shape:

DELETE FROM table_name
WHERE condition;
  • DELETE FROM names the table to remove rows from.
  • WHERE holds the condition. DELETE removes every row that matches the condition.
  • Without WHERE, the statement deletes every row of the table.

Create two sample tables, customers and orders, and fill them:

CREATE TABLE customers (
  customer_id int PRIMARY KEY,
  name        varchar(20),
  status      varchar(10)
);

INSERT INTO customers VALUES
  (1, 'Ada',   'open'),
  (2, 'Grace', 'closed'),
  (3, 'Linus', 'open');

CREATE TABLE orders (
  order_id    int PRIMARY KEY,
  customer_id int,
  amount      int,
  status      varchar(10),
  FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);

INSERT INTO orders VALUES
  (10, 1, 40, 'shipped'),
  (11, 2, 15, 'cancelled'),
  (12, 1, 90, 'shipped'),
  (13, 3,  5, 'cancelled'),
  (14, 2, 25, 'shipped'),
  (15, 3, 60, 'shipped');

The foreign key on customer_id links each order to a row of customers. On SQLite, run PRAGMA foreign_keys = ON; first. SQLite checks foreign keys only after this PRAGMA.

Run the examples in order. Each DELETE changes the orders table, and the next DELETE runs on the rows that remain.

Every query result below comes from PostgreSQL 18, MySQL 9.1, and SQLite 3.50. All three returned the same rows unless an engine is named. The SQL Server statements follow Microsoft's documentation only.

Practice this in Lesson 13 of the SQL tutorial: you run INSERT, UPDATE and DELETE on a shop database in your browser, and each exercise is checked.

DELETE with WHERE

A DELETE with a WHERE on the primary key removes one row. For example, you can delete order 12 by its order_id.

DELETE FROM orders
WHERE order_id = 12;

SELECT * FROM orders now returns:

order_idcustomer_idamountstatus
10140shipped
11215cancelled
1335cancelled
14225shipped
15360shipped

Because order_id is the primary key, order_id = 12 matches one order only. A condition on another column, such as amount = 90, can match more rows than you meant.

SELECT before DELETE

A SELECT with the same WHERE as a DELETE returns the rows that the DELETE would remove. For example, you can list the cancelled orders before you delete them.

SELECT * FROM orders
WHERE status = 'cancelled';
order_idcustomer_idamountstatus
11215cancelled
1335cancelled
DELETE FROM orders
WHERE status = 'cancelled';

SELECT * FROM orders now returns:

order_idcustomer_idamountstatus
10140shipped
14225shipped
15360shipped

The SELECT returned orders 11 and 13, and the DELETE removed the same two orders.

The orders table with five rows; WHERE status = 'cancelled' matches orders 11 and 13, which the SELECT returns and the DELETE with the same WHERE removes, while orders 10, 14 and 15 stay

A DELETE takes the same conditions as a SELECT, such as IN, BETWEEN, or <. The SQL WHERE clause explains each of them.

DELETE with a subquery or JOIN

A subquery is a SELECT inside the WHERE, and it lets a DELETE choose rows by a value in another table. For example, you can delete the orders of the closed customers.

DELETE FROM orders
WHERE customer_id IN (
  SELECT customer_id
  FROM customers
  WHERE status = 'closed'
);

SELECT * FROM orders now returns:

order_idcustomer_idamountstatus
10140shipped
15360shipped

Grace is the only closed customer, so the subquery returns her customer_id, 2. Her order 11 was already gone, so this DELETE removed only order 14.

On PostgreSQL, MySQL, and SQL Server, you can also write this DELETE as a join, which reads orders and customers together. On PostgreSQL, you name the second table in USING:

DELETE FROM orders
USING customers
WHERE orders.customer_id = customers.customer_id
  AND customers.status = 'closed';

On MySQL and SQL Server, you write the table name after DELETE, then join the second table after FROM:

DELETE orders
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id
WHERE customers.status = 'closed';

Use one join form instead of the subquery. Each join form deletes the same order 14.

On SQLite, use the subquery. SQLite has no join form of DELETE.

BEGIN and ROLLBACK

BEGIN starts a transaction: a group of changes that you keep with COMMIT or undo with ROLLBACK. For example, you can delete every order, check the empty table, and then bring the orders back.

BEGIN;
DELETE FROM orders;
SELECT * FROM orders;
ROLLBACK;
SELECT * FROM orders;

After ROLLBACK, the last SELECT returns:

order_idcustomer_idamountstatus
10140shipped
15360shipped

The DELETE has no WHERE, so the first SELECT returned no rows. ROLLBACK brought both orders back.

To keep the delete, end with COMMIT in place of ROLLBACK. Commit only after the SELECT inside the transaction shows the rows that you expect.

A DELETE without BEGIN is committed as soon as it succeeds on PostgreSQL, MySQL, and SQLite. After the commit, only a backup brings the deleted rows back.

On SQL Server, write BEGIN TRANSACTION in place of BEGIN. SQL Server reads BEGIN by itself as the start of a block of statements, not of a transaction.

RETURNING

RETURNING makes a DELETE return the deleted rows. For example, you can delete order 15 and see its row in the result.

DELETE FROM orders
WHERE order_id = 15
RETURNING *;

PostgreSQL and SQLite return the deleted row:

order_idcustomer_idamountstatus
15360shipped

SELECT * FROM orders now returns:

order_idcustomer_idamountstatus
10140shipped

SQLite has had RETURNING since version 3.35. On MySQL, run the DELETE without RETURNING *. MySQL rejects RETURNING with a syntax error.

On SQL Server, use OUTPUT DELETED.* in place of RETURNING: DELETE FROM orders OUTPUT DELETED.* WHERE order_id = 15.

DELETE, TRUNCATE and DROP

TRUNCATE removes every row of a table. DROP TABLE removes the table itself. For example, you can empty the orders table with TRUNCATE and then count its rows.

TRUNCATE TABLE orders;
SELECT count(*) AS order_count FROM orders;
order_count
0

TRUNCATE removed order 10. The count still works, because TRUNCATE kept the orders table. After DROP TABLE orders, the same count fails, because the table is gone.

On SQLite, run DELETE FROM orders; instead. SQLite has no TRUNCATE. When a DELETE has no WHERE, SQLite empties the table without reading each row.

DELETETRUNCATEDROP TABLE
RemovesThe rows that matchEvery rowThe table and its rows
Takes a WHEREYesNoNo
Keeps the tableYesYesNo

TRUNCATE empties a large table faster than a DELETE without WHERE, because TRUNCATE does not read the rows one by one. Here is how TRUNCATE behaves on three engines:

TRUNCATEPostgreSQL 18MySQL 9.1SQL Server 2022
Undone by ROLLBACKYesNo, it commits at onceYes
Auto-increment counterKept, unless you add RESTART IDENTITYResetReset to its start value
Fires DELETE triggersNoNoNo
Allowed on a table that a foreign key referencesNoNoNo

On PostgreSQL, MySQL, and SQL Server, DELETE keeps the auto-increment counter where it was. Right now orders has no rows, and TRUNCATE TABLE customers still fails, because orders references customers.

What goes wrong

Put two orders back into the empty orders table:

INSERT INTO orders VALUES
  (10, 1, 40, 'shipped'),
  (16, NULL, 10, 'shipped');

A foreign key rejects the delete

Ada still has order 10. Try to delete Ada:

DELETE FROM customers
WHERE customer_id = 1;

PostgreSQL rejects the DELETE, because order 10 would lose its customer:

ERROR:  update or delete on table "customers" violates foreign key constraint "orders_customer_id_fkey" on table "orders"
DETAIL:  Key (customer_id)=(1) is still referenced from table "orders".

MySQL reports "Cannot delete or update a parent row: a foreign key constraint fails". SQLite reports "FOREIGN KEY constraint failed". Without the PRAGMA, SQLite skips the check and deletes Ada.

To delete Ada, delete her orders first. Or declare the foreign key of orders with ON DELETE CASCADE after REFERENCES. Then the same DELETE removes Ada and her orders together.

What Is a Foreign Key? explains how to choose between these two fixes.

NOT IN and NULL

The DELETE below is meant to remove the customers who have no orders:

DELETE FROM customers
WHERE customer_id NOT IN (
  SELECT customer_id
  FROM orders
);

SELECT * FROM customers still returns all three:

customer_idnamestatus
1Adaopen
2Graceclosed
3Linusopen

The subquery returns 1 and NULL, from orders 10 and 16. A comparison with NULL is never true, so NOT IN is never true for any customer, and the DELETE removes nothing.

SQL NULL values explains why. Use NOT EXISTS instead. NOT EXISTS asks, for each customer, whether an order with that customer_id exists:

DELETE FROM customers
WHERE NOT EXISTS (
  SELECT 1
  FROM orders
  WHERE orders.customer_id = customers.customer_id
);

The customers table now holds:

customer_idnamestatus
1Adaopen

Grace and Linus have no orders, so the DELETE removed both customers. Ada remains, because order 10 belongs to her. Order 16 matches no customer, so its NULL changes nothing.

Run DELETE statements in DbSchema

The SQL Editor in DbSchema runs a DELETE inside an open transaction, as BEGIN does.

  1. Press Editor in the toolbar and choose SQL Editor.
  2. Type the SELECT with its WHERE, press Run, and read the rows in the result grid.
  3. Change SELECT * to DELETE, and press Run again.
  4. Press Commit to keep the change, or Rollback to undo it.
A DELETE of the cancelled orders in the DbSchema SQL Editor after Run, with Modified 2 rows under the editor and the Commit and Rollback buttons in its toolbar

Run changes the data in the connected database, not the DbSchema design model.

In the DbSchema diagram, each foreign key is a line between two tables. Before you delete from a table, follow its lines to see which tables point at it.

Download DbSchema at https://dbschema.com/download.html and connect it to PostgreSQL, MySQL, or SQLite. Run the sample statements with Run Script in the SQL Editor, then try each DELETE. The free Community Edition includes the database connection, the diagram, and the SQL Editor with its Commit and Rollback buttons.

DbSchema ER diagram designer DbSchema ER diagram designer

Design and visualize
your database schema

Edit referenced records
in related tables

Query your data
visually too

Reuse the SQL
generated

Free Download