Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SQL Dictionary
  3. DELETE

DELETE

The SQL statement for deleting rows from a table. If you do not narrow down the target rows with a WHERE clause, all rows will be deleted, so always verify before executing.

Syntax

-- Deletes rows that match the condition.
DELETE FROM table_name WHERE condition;

-- Deletes all rows in the table (WHERE omitted).
DELETE FROM table_name;

Syntax List

SyntaxDescription
DELETE FROM table_name WHERE conditionDeletes rows that match the condition. Returns the number of rows deleted.
DELETE FROM table_nameDeletes all rows in the table. This is the behavior when WHERE is omitted.
DELETE ... LIMIT countA MySQL extension. Limits the maximum number of rows to delete.

Sample Code

-- Deletes the employee with ID 5.
DELETE FROM employees WHERE id = 5;

-- Deletes all employees who have a retirement date set.
DELETE FROM employees WHERE retire_date IS NOT NULL;

-- Deletes logs older than a specific date.
DELETE FROM access_logs WHERE created_at < '2023-01-01';

-- Deletes products that no one has ordered (using a subquery).
DELETE FROM products
WHERE product_id NOT IN (SELECT DISTINCT product_id FROM orders);

-- Check the number of target rows before deleting (safe approach).
SELECT COUNT(*) FROM employees WHERE retire_date IS NOT NULL;
-- After confirming the count, run the DELETE.
DELETE FROM employees WHERE retire_date IS NOT NULL;

Execution Result

-- Example result of: DELETE FROM employees WHERE retire_date IS NOT NULL;
-- Query OK, 3 rows affected (0.02 sec)
-- (Indicates that 3 rows were deleted.)

Database-Specific Syntax

The basic DELETE FROM ... WHERE syntax works across all major databases.

DELETE FROM employees WHERE retire_date IS NOT NULL;

Limiting the number of deleted rows with DELETE ... LIMIT is a MySQL-only feature. In PostgreSQL, you can use DELETE ... USING to specify the deletion target with a JOIN-like syntax instead of a subquery.

-- MySQL (limit the number of rows deleted)
DELETE FROM access_logs WHERE created_at < '2023-01-01' LIMIT 1000;

-- PostgreSQL (use USING clause to join and filter the target)
DELETE FROM order_items
USING orders
WHERE order_items.order_id = orders.order_id AND orders.status = 'cancelled';

PostgreSQL supports DELETE ... RETURNING, which returns the data from deleted rows. This is useful for confirming what was deleted or for logging purposes.

-- PostgreSQL (return deleted rows)
DELETE FROM employees WHERE retire_date IS NOT NULL RETURNING id, name;

Notes

DELETE removes rows, but the table itself and its structure (column definitions, indexes, etc.) remain intact. Deleted rows can also be restored with ROLLBACK as long as the operation is within a transaction.

Omitting the WHERE clause deletes all rows. Always verify the number of target rows with SELECT COUNT(*) or similar before executing. It is strongly recommended to run operations in a production environment within a transaction.

If you need to delete all rows from a table, consider using TRUNCATE, which is faster and does not write to the transaction log. Note, however, that TRUNCATE cannot be protected by a transaction.

If you find any errors or copyright issues, please .