Search code snippets, questions, articles...

SQL Query to delete rows that have different Ids or column values

SQL Query examples that you can use to delete multiple rows from a table that have different column values.
-- Using IN Clause
DELETE from table_name WHERE id IN (2,6,9,8);
-- The above query will delete rows which has values - 2,6,9,8 in id name column

-- Using Between Operator
DELETE FROM table_name WHERE id BETWEEN 20 AND 50;
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

In the code snippet, we want to delete rows of a column named id which has different values. We are using IN clause of SQL to delete them. The basic syntax will be as below.

DELETE from table_name WHERE column_name IN (value_1, value_2, ...);

To delete rows that do not contain specific column values. The syntax will be as below.

DELETE FROM table_name WHERE column_name NOT IN (value_1, value_2, ...);
DELETE FROM table_name
WHERE id BETWEEN 3 AND 30;
BETWEEN clause also can be used to delete multiple rows of a table. You just need to pass top and bottom values to it.
In the above code, we are deleting rows from a table that has a column named id and deleting between values from 3 to 30.
DELETE FROM table_name
WHERE id >= 10 AND id <= 30;
Like BETWEEN clause range can be passed to WHERE clause where we can provide from which range you want to delete rows.
In the above code snippet, we are deleting rows that have id values greater than or equals to 10 and less than or equals to 30.
DELETE FROM table_name 
WHERE id NOT IN (10, 11, 12);
The code will delete all rows that do not have 10, 11, or 12 values as id named column.
Was this helpful?
0 Comments