Querying Data with SELECT: Retrieving Information
Deleting data is different from inserting or updating it.
Try it: INSERT, UPDATE and DELETE
How INSERT builds a new row from its column list (omitted columns get the generated id, their DEFAULT or NULL), how UPDATE … SET and DELETE test WHERE on every row and change only the matches — or every row when WHERE is missing — and how cur.rowcount and commit() report and keep the change.
How it works
- The statement is sent with ? placeholders and a tuple of values: cur.execute(sql, params).
- INSERT: the listed columns get the values in order; an unlisted INTEGER PRIMARY KEY gets max(id) + 1, an unlisted column gets its DEFAULT (or NULL). A listed id that already exists raises IntegrityError.
- UPDATE / DELETE: WHERE is evaluated on each row; only rows where it is TRUE match (NULL does not). No WHERE means every row matches. The same WHERE in a SELECT shows the target rows first.
- UPDATE applies every SET assignment to each matched row, reading the row's old values; DELETE removes the matched rows. Matching nothing is not an error.
- cur.rowcount is the number of rows inserted, changed or removed.
- The change is pending until conn.commit(); conn.close() without commit() rolls it back, so a new connection still sees the old table.
Default run (11 steps): UPDATE track SET plays = ? WHERE title = ? with parameters (16, 'My Way'). The track table starts with 5 rows. … conn.commit() makes the change permanent. A new connection now sees 5 rows.
Simplified: A toy relational engine in the browser, not SQLite itself: statements are built from the controls (no SQL text is parsed or executed) and tables have a few rows. Results were checked against Python's sqlite3 (SQLite 3.49). One table track(id INTEGER PRIMARY KEY, title TEXT, plays INTEGER DEFAULT 0) of up to 8 rows, one WHERE comparison, text limited to printable ASCII. The generated id is max(id) + 1, SQLite's rule for an INTEGER PRIMARY KEY without AUTOINCREMENT.
Loading the simulation…
Deletion Requires Deliberate Targeting
Deleting data is different from inserting or updating it. Inserting adds a row, and updating modifies an existing value. DELETE removes rows from a table, so the operation demands careful targeting. In most practical situations, recovering a deleted row requires restoring it from a backup.
The WHERE clause is the main safety mechanism in a DELETE statement. It acts as a filter that identifies which rows should be removed and which should remain.
Filtering Rows with WHERE
A DELETE statement removes rows from a table. Adding a WHERE clause gives the database a condition to evaluate for each row. Rows that satisfy the condition are selected for deletion; rows that do not satisfy it remain in the table.
Tracing a Matching Condition
Removing tracks titled My Way
Consider a Track table containing Bohemian Rhapsody, My Way, Imagine, and a second My Way entry. What remains after applying DELETE with the condition title = 'My Way'?
- 1Check the first row
For Bohemian Rhapsody, title = 'My Way' is false, so the row remains.
- 2Check the matching rows
For each My Way entry, title = 'My Way' is true, so both matching rows are selected for deletion.
- 3Check the remaining nonmatching row
For Imagine, the condition is false, so that row remains.
After the statement executes, only Bohemian Rhapsody and Imagine remain in the table.
The condition is evaluated independently for each row. This means a single DELETE statement can remove more than one row when multiple rows satisfy the same WHERE condition.
The Empty WHERE Clause Trap
When DELETE FROM Track is executed without a WHERE clause, there is no condition to evaluate. Every row matches the implicit meaning of the statement, so every row is deleted and the table becomes empty.
Checking Before Deleting
Before executing DELETE, test the WHERE condition with SELECT. This lets you verify which rows the condition identifies before you remove them. The check is especially important because the WHERE clause determines the complete set of rows affected by the deletion.
- 1Use SELECT to test the condition before deletion.
- 2Verify that the rows identified are exactly the rows intended for removal.
- 3Execute DELETE with the specific WHERE condition.
- 4Call
commit()to finalize the pending deletion.
Finalizing the Transaction
In many database systems, DELETE does not immediately make the removal permanent. Instead, the rows are marked for deletion within a transaction. Calling commit() tells the database to finalize pending changes and write them to disk, making the deletion permanent and visible to other users or programs accessing the database.
Common Deletion Mistakes
Common mistake Omitting the WHERE clause
What it looks likeDELETE FROM Track;Why it failsWithout a condition, every row in Track is eligible for deletion, leaving the table empty.
Do this insteadInclude a specific WHERE condition that identifies the intended rows.
Common mistake Skipping the SELECT check
Why it failsYou may not notice that the condition identifies more rows than intended until after DELETE executes.
Do this insteadTest the WHERE condition with SELECT and verify the target rows before deleting.
Common mistake Forgetting to call commit()
Why it failsThe deletion may remain a pending transaction rather than becoming a finalized database change.
Do this insteadCall commit() after executing DELETE unless the database library or framework explicitly handles committing automatically.
Common mistake Assuming a matching title identifies only one row
What it looks likeDELETE FROM Track WHERE title = 'My Way';Why it failsEvery row whose title equals My Way matches the condition, so multiple rows can be deleted.
Do this insteadUse SELECT first to check how many rows satisfy the condition.
Practice the Safe Sequence
A Track table contains several songs. You intend to remove only the rows whose title is My Way. Describe the sequence you should follow before the deletion becomes a permanent database change.
Hints
- Begin by checking the target rows with SELECT.
- Use a WHERE condition that identifies the intended title.
- After DELETE, call
commit()to finalize the change.
What do you think happens?
What happens if DELETE FROM Track is executed without a WHERE clause?
Reveal answer
Every row in Track is deleted.
Without a WHERE clause, the database has no condition to evaluate, so every row matches the statement.
Key Takeaways
- 1DELETE removes rows from a table.
- 2WHERE filters the rows so that only matching records are selected for deletion.
- 3Leaving out WHERE causes every row in the table to be deleted.
- 4Use SELECT before DELETE to verify the rows targeted by the condition.
- 5Call
commit()after DELETE to finalize the deletion and save the database change.
At a glance
- A DELETE statement removes rows, while its WHERE clause determines which rows match.
- The WHERE condition is evaluated for each row, so one DELETE can remove multiple matching rows.
- A DELETE statement without WHERE deletes every row in the table.
- Testing the condition with SELECT is a practical safeguard before deletion.
- Calling
commit()finalizes the pending deletion and makes the change permanent.