Which SQL statement is used to delete data from a table?

Understanding SQL DELETE Statement

The SQL DELETE statement is used to remove rows from a table. Unlike the TRUNCATE statement which removes all rows, DELETE allows for more selectivity through the use of a WHERE clause. The REMOVE does not exist in SQL, confirming that the correct answer to the question is indeed the DELETE statement.

Take an example where we have an employees table, and we want to delete a specific employee whose emp_id is '1001'. The SQL statement would look like this:

DELETE FROM employees
WHERE emp_id = '1001';

The above command deletes only the specific row where 'emp_id' matches '1001'. If you want to delete all the rows from the employees table, you can use the DELETE statement without the WHERE clause - but be cautious as this will eliminate all your data:

DELETE FROM employees;

However, using the DELETE statement without a WHERE clause may not be a recommendable action, especially in a production database because it can lead to data loss. Instead, it is often advisable to use the TRUNCATE statement to remove all rows, since TRUNCATE is faster and uses fewer system and transaction log resources.

While the DELETE statement deletes rows one at a time and records an entry in the transaction log for each deleted row, TRUNCATE deallocates data page by page, making it a more efficient solution when all rows are to be removed.

But as per the original question, when it comes to deleting selective data from SQL table, the DELETE statement is the apt tool. Always remember to ensure you have a good handle on your WHERE clauses to accurately target the data you need to delete, especially when dealing with vital data. In addition, before running any DELETE statement, it's a good practice to run a SELECT statement first with the same WHERE clause to confirm you're deleting the intended records.

Do you find this helpful?