AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL DELETE |
|
SQL > SQL Commands >
Delete From Statement
The DELETE FROM statement in SQL is used to remove records from a table. The SQL DELETE FROM statement permanently removes rows from a table. Always include a WHERE clause to target specific rows — omitting it will delete every row in the table. Run the equivalent SELECT first to verify which rows will be affected.
Please note that the DELETE FROM command cannot delete any rows of data that would violate FOREIGN KEY or other constraints. SyntaxThe syntax for the DELETE FROM statement is as follows:
The WHERE clause is important here. Without specifying a condition, all records from the table will be deleted. "Condition" can be simple (such as "Sales > 500") or complex (such as from the result of a subquery). ExamplesTwo examples of how to use the DELETE FROM statement are shown below. Example 1: DELETE FROM using a simple conditionWe use the following table as the starting point. Table Store_Information
We decide not to keep any information on Los Angeles in this table. To accomplish this, we type the following SQL:
Now the table becomes, Table Store_Information
Example 2: DELETE FROM using the results from a subqueryIn Example 1, the criteria we use to determine which rows to delete is quite simple. We can also use a more complex condition. Below is an example where we use a subquery as the condition. Assume we have the following two tables: Table Store_Information
Table Geography
We want to remove data for all stores in the East region from Store_Information (assuming that a store is either in the East region or the West region—it cannot be in more than one region). We use the following SQL statement to accomplish this:
Upon execution, the Store_Information table becomes,
If we leave out the WHERE clause in a DELETE FROM command, we will delete all rows from the table. Most times, this is not what we intend to do. To prevent this, it is a best practice in database management to always run the corresponding SELECT statement first to make sure the rows selected are the ones we intend to remove from the table. This can be done by replacing "DELETE" with "SELECT *". Frequently Asked QuestionsWhat is the difference between DELETE and DROP in SQL?DELETE removes rows from a table while keeping the table structure intact. DROP TABLE removes the entire table — both its structure and all its data — permanently. Use DELETE to remove specific data, and DROP only when you want to completely eliminate the table. Can I DELETE rows based on a condition involving multiple tables?Yes. Use a subquery in the WHERE clause with IN or EXISTS: How can I undo an accidental DELETE?If the DELETE was run inside an open transaction, you can ROLLBACK to undo it. If already committed, you'll need to restore from a backup or transaction log. This is why wrapping bulk DELETEs in a BEGIN TRANSACTION before committing is strongly recommended. Why does DELETE fail with a foreign key constraint error?A FOREIGN KEY constraint prevents deleting a parent row when child rows in another table still reference it. You must either delete the child rows first, update the foreign key to NULL (if allowed), or use ON DELETE CASCADE if the database schema supports it. ExercisesFor the questions below, we use the following table as the starting point:Table Clients
1. Which of the following SQL statements is valid? (There may be more than one answer)
2. How many rows are deleted after the following SQL statement is executed?
3. What is the effect of the following SQL?
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.