In SQL, the WHERE
clause is used alongside the DELETE
statement to delete a specified record from a table.
The WHERE
clause is commonly used with comparison operators. These include:
=
(equal to operator)!=
(not equal to operator)>
(greater than operator)<
(less than operator)>=
(greater than or equal to operator)<=
(less than or equal to operator)It is worth noting that:
WHERE
clause has the primary key with the =
operator, then only one record can be affected —if they are found.WHERE
clause has the non-primary key with the =
operator, then multiple records can be affected —if they are found.<
or >
, multiple records will be affected, if they are found.The syntax of the DELETE
statement is given below:
DELETE FROM name_of_table
WHERE condition;
The database we will be working with is shown below:
CustomerID | Name | Country |
1 | Theo David | U.S.A |
2 | Peter Mark | United Kingdom |
3 | Antonio Clark | Mexico |
4 | Benjamin Monday | U.S.A |
In the code written below, we will use the WHERE
clause alongside the DELETE
statement to delete a specific part of the Customers
table.
-- using the WHERE clause alongside the SELECT statementDELETE FROM CustomersWHERE CustomerName='Benjamin Monday';-- printing the modified tableSELECT * FROM Customers
Customers
table and populate it with values, according to the defined database schema on our platform.WHERE
clause and the DELETE
statement with a condition of where the CustomerName
is Benjamin Monday
.Customers
.Note: Notice from the output of the code that the record which has the
CustomerName
asBenjamin Monday
has been deleted.