📜  sql delete where in - SQL (1)

📅  最后修改于: 2023-12-03 15:35:04.377000             🧑  作者: Mango

SQL Delete Where In

The DELETE statement in SQL is used to delete one or more rows from a table. The WHERE clause is used to specify which rows to delete based on a specific condition. The IN operator is used to specify a list of values to match against a column.

The syntax for the DELETE statement with WHERE IN is as follows:

DELETE FROM table_name
WHERE column_name IN (value1, value2, value3, ...);

This will delete all rows from the table_name where the column_name matches any of the specified values.

For example, consider the following employee table:

| id | first_name | last_name | salary | |----|------------|-----------|--------| | 1 | John | Smith | 50000 | | 2 | Jane | Doe | 60000 | | 3 | Bob | Johnson | 70000 | | 4 | Alice | Williams | 55000 |

To delete all employees with a salary of $50,000 or $70,000, the SQL query would be:

DELETE FROM employee
WHERE salary IN (50000, 70000);

This would result in the following table:

| id | first_name | last_name | salary | |----|------------|-----------|--------| | 2 | Jane | Doe | 60000 |

Note that only the row with id=1 is deleted because it is the only row with a salary of $50,000 or $70,000.

In conclusion, the SQL DELETE WHERE IN statement is a useful tool for deleting specific rows from a table based on a list of values. Use this statement with caution as it permanently deletes data from the table and cannot be undone.