Academic Block

SQL DELETE Get Certified

Learn how to remove records from a database table using the SQL DELETE statement.

What is the DELETE Statement?

The DELETE statement is used to remove existing records from a table. It can delete a single row, multiple rows, or all rows, depending on the condition specified in the WHERE clause.

Basic DELETE Syntax

The basic syntax specifies the table to delete from and a condition to identify which rows should be removed.

What this code does: Shows the general form of a DELETE statement. DELETE FROM names the table, and WHERE describes which rows should be removed. Every row that satisfies the condition is deleted, and rows that do not satisfy it are left untouched.

DELETE FROM table_name
WHERE condition;

Deleting a Single Record

To remove one specific row, use a WHERE condition that uniquely identifies that record.

What this code does: Creates an employees table with four people and then deletes the one named John Smith. Because only one row matches name = 'John Smith', exactly one record is removed. The final SELECT shows the three remaining employees: Emily Johnson, Robert Davis and Jessica Miller.

DROP TABLE IF EXISTS employees;

CREATE TABLE employees (
  id INTEGER,
  name TEXT,
  department TEXT
);

INSERT INTO employees (id, name, department)
VALUES
  (1, 'John Smith', 'Sales'),
  (2, 'Emily Johnson', 'IT'),
  (3, 'Robert Davis', 'HR'),
  (4, 'Jessica Miller', 'Finance');

DELETE FROM employees
WHERE name = 'John Smith';

SELECT * FROM employees;

Deleting Multiple Records

A WHERE condition that matches more than one row will remove every matching record in a single statement.

What this code does: Removes every employee who belongs to the ‘Temp’ department with one statement. Sarah Wilson and Linda Taylor both match the condition, so both rows are deleted together. David Moore (IT) and Daniel Anderson (HR) do not match and remain in the table.

DROP TABLE IF EXISTS employees;

CREATE TABLE employees (
  id INTEGER,
  name TEXT,
  department TEXT
);

INSERT INTO employees (id, name, department)
VALUES
  (1, 'Sarah Wilson', 'Temp'),
  (2, 'David Moore', 'IT'),
  (3, 'Linda Taylor', 'Temp'),
  (4, 'Daniel Anderson', 'HR');

DELETE FROM employees
WHERE department = 'Temp';

SELECT * FROM employees;

The Danger of Omitting WHERE

If the WHERE clause is omitted, DELETE will remove every row in the table. The table structure remains, but all data inside it is gone.

What this code does: Runs DELETE FROM employees with no WHERE clause, so all four rows are removed. The final query counts the rows that are left and returns 0. The employees table itself still exists with all of its columns, it is just empty. Always double-check for a missing WHERE before running a delete on real data.

DROP TABLE IF EXISTS employees;

CREATE TABLE employees (
  id INTEGER,
  name TEXT,
  department TEXT
);

INSERT INTO employees (id, name, department)
VALUES
  (1, 'John Smith', 'Sales'),
  (2, 'Emily Johnson', 'IT'),
  (3, 'Robert Davis', 'HR'),
  (4, 'Jessica Miller', 'Finance');

-- Warning: this deletes ALL rows in the table
DELETE FROM employees;

SELECT COUNT(*) AS remaining_rows
FROM employees;

Deleting with a Subquery

A subquery can be used inside the WHERE clause to delete rows based on conditions found in another table.

What this code does: First the subquery looks in the departments table and returns the ids of every department marked as closed (closed = 1), which here is only department 20. The outer DELETE then removes every employee whose department_id is in that list. Ashley Brown and Christopher Lee are deleted, and Michael Thomas and Amanda White, who work in open departments, remain.

DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS departments;

CREATE TABLE departments (
  id INTEGER,
  name TEXT,
  closed INTEGER
);

INSERT INTO departments (id, name, closed)
VALUES
  (10, 'Engineering', 0),
  (20, 'Regional Office', 1),
  (30, 'Marketing', 0);

CREATE TABLE employees (
  id INTEGER,
  name TEXT,
  department_id INTEGER
);

INSERT INTO employees (id, name, department_id)
VALUES
  (1, 'Michael Thomas', 10),
  (2, 'Ashley Brown', 20),
  (3, 'Christopher Lee', 20),
  (4, 'Amanda White', 30);

DELETE FROM employees
WHERE department_id IN (
  SELECT id FROM departments WHERE closed = 1
);

SELECT * FROM employees;

DELETE vs TRUNCATE

TRUNCATE TABLE also removes all rows, but it is faster than DELETE because it does not log individual row deletions. However, TRUNCATE cannot be filtered with a WHERE clause and typically cannot be rolled back in some database systems.

What this code does: Shows the TRUNCATE TABLE statement that databases such as MySQL, PostgreSQL and SQL Server use to empty a table quickly. SQLite does not support TRUNCATE, so this example is for reference only and has no Test button. In SQLite, use DELETE FROM employees; (shown in the previous section) to remove all rows.

TRUNCATE TABLE employees;

Common DELETE Clauses

Clause
Purpose
Example
DELETE FROM
Specify the table to remove rows from
DELETE FROM employees
WHERE
Limit which rows are deleted
WHERE name = 'John Smith'
TRUNCATE TABLE
Quickly remove all rows (not supported in SQLite)
TRUNCATE TABLE employees

Advantages of DELETE

  • Removes unwanted or outdated records precisely.
  • Can target a single row, several rows, or an entire table.
  • Supports subqueries for conditional deletion.
  • Keeps table structure intact while removing data.
  • Works with transactions, allowing changes to be rolled back if supported.

Best Practices

  • Always use a WHERE clause unless you intend to delete every row.
  • Run a SELECT with the same WHERE condition first to confirm affected rows.
  • Back up important data before running large DELETE statements.
  • Use transactions for critical deletions when supported.
  • Consider TRUNCATE only when you need to clear an entire table quickly (not available in SQLite).

The DELETE statement lets you remove exactly the records you no longer need. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.