DELETE
DELETE removes rows from a table. Like UPDATE, it needs a WHERE clause to target specific rows without one, it removes everything. DELETE FROM students WHERE id = 2; removes only the row where id equals 2. The table itself still exists afterward, with its structure intact only that one row is gone. DELETE FROM students; with no WHERE clause removes every row in the table, leaving it empty but still existing (this is different from DROP TABLE, covered later, which removes the table itself entirely, structure and all). Just like UPDATE, the safest habit is to first run the exact same condition as a SELECT (SELECT * FROM students WHERE id = 2;) to confirm exactly which row(s) you're about to remove, before switching SELECT * to DELETE.
Why this matters
- ·DELETE lets you cleanly remove rows that are no longer needed, like a cancelled order or a closed account.
- ·A precise WHERE clause targets exactly the row (or rows) meant to be removed.
- ·The table's structure survives a DELETE you can keep inserting new rows into it afterward.
Common mistakes
- ·Running DELETE without a WHERE clause removes every row in the table this is one of the most serious mistakes possible in SQL.
- ·DELETE has no built-in undo once committed, the removed data is gone unless you have a backup.
- ·A WHERE condition that's broader or looser than intended can delete far more rows than you meant to.
Query OK, 1 row affected +----+------+-----+ | id | name | age | +----+------+-----+ | 1 | Ali | 17 | +----+------+-----+ 1 row in set
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Frequently asked questions
DELETE removes rows but keeps the table's structure intact. DROP TABLE (covered later) removes the entire table, including its structure.
Practice questions
Question 1
Write a DELETE that removes the student with id 1.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Question 2
Write a SELECT that confirms which row would be deleted by WHERE age > 20, before actually deleting it.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 3
