Deleting data in SQLite
is done using the SQL command DELETE FROM. You can use the SQL DELETE
statement to remove one or more rows from a table.
Syntax
The syntax for the SQLite DELETE statement is as follows:
DELETE FROM table_name WHERE condition;
In this syntax if you omit the WHERE clause, SQLite will delete all records in the table.
Examples
Delete Single Row
You can use SQLite DELETE statement to delete a single row from a table. For example:
DELETE FROM customers WHERE id = 9;
This SQLite DELETE example would delete the row from the customers table where the id is 9.
Delete From Select
You can also use SQLite DELETE statement to remove rows from a table based on information stored in another table. For example:
DELETE FROM customers WHERE id IN (SELECT customer_id FROM orders WHERE status = 'Shipped');
This SQLite DELETE statement would delete all customers from the customers table where there is a row in the orders table with a matching customer_id and a status of Shipped.
Delete Multiple Rows
You can use SQLite DELETE statement to delete multiple rows from a table. For example:
DELETE FROM customers WHERE id IN (4, 5);
This SQLite DELETE example would delete the rows from the customers table where the id is 4 or 5.
Let’s say you want to delete customers whose city names contain the word San:
DELETE FROM customers WHERE city LIKE '%San%';
After executing the command above, all records from the customers table that contain the San value in the city column will be deleted.