For the examples in this article, let's suppose we have a table with unique ids that we're going to be using to delete several rows in a single query. You can, of course, use other conditions/columns as well.
Deleting All Rows as Specified
IN clause, we can specify multiple row ids to delete. For example, the following query would delete rows with ids equal to
DELETE from `tablename` WHERE `id` IN (1, 5 , 7);
Deleting All Rows Except Some
NOT IN clause we can delete all rows except some like so:
DELETE FROM `tablename` WHERE `id` NOT IN (1, 5, 7);
This would delete all rows except rows with ids equal to
Deleting All Rows in a Range
We can delete a number of rows between a range in the following way:
DELETE FROM `tablename` WHERE `id` >= 3 AND `id` <= 10;
This is equivalent to using the
BETWEEN operator like so:
DELETE FROM `table_name` WHERE `id` BETWEEN 3 AND 10;
Both these queries would delete all rows with ids between
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.