MySQL DELETE Query

DELETE removes rows from a table.

DELETE FROM students WHERE name = 'Neha';
MySQL says

Query OK, 1 row affected (0.01 sec)

SELECT COUNT(*) AS total FROM students;
Result
total
5

Note: Always use WHERE. DELETE FROM students; deletes all the rows. Run a SELECT with the same WHERE first, to see what will be deleted.

Put the row back

INSERT INTO students (id, name, city, marks, course_id)
VALUES (6, 'Neha', 'Mumbai', 45, 2);

DELETE, TRUNCATE and DROP

CommandWhat it removesCan use WHERE?
DELETE FROM t WHERE ...Some or all rows. Table staysYes
TRUNCATE TABLE tAll rows, fast. Table stays and the counter restartsNo
DROP TABLE tThe whole table, with its structureNo

Read about DROP TABLE.


Summary

  • DELETE FROM table WHERE condition; removes rows.
  • Without WHERE every row is deleted.
  • Next: DROP TABLE.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.