Deleting Data


Now that you know how to insert data, let’s learn how to delete data from a table. We will use the Countries table again, to demonstrate how to delete rows. To delete all European countries from the Countries table, use this SQL query:

DELETE FROM Countries
WHERE Continent = ‘Europe’

The DELETE FROM clause is followed by the name of the table, from where we are deleting data. The WHERE clause instructs the SQL engine to delete only rows which have value ‘Europe’ in the Continent column. What will happen if we omit the WHERE clause and execute this statement:

DELETE FROM Countries

The statement above will delete all rows in the Countries table. The SQL DELETE statement is very powerful and you cannot reverse it, so be extremely careful and think twice before using it.

There is another SQL keyword that can be used to delete data from tables – the TRUNCATE keyword. The following statement will delete all rows from the Countries table:

TRUNCATE TABLE Countries

The truncate table statement cannot be used along with the WHERE clause, and it always deletes all rows in the table.