SQL - Delete Query - Remove Records

The DELETE statement in SQL is used to delete existing records from a table. It allows you to remove one or more rows from a table based on specified conditions. The DELETE statement is commonly used to remove unwanted or obsolete data from a table.

Syntax:

The basic syntax of the DELETE statement is as follows:

DELETE FROM table_name
WHERE condition;

Example:

Let's assume we have a "Customers" table with the following columns:

CustomerID (unique identifier for each customer)

FirstName (first name of the customer)

LastName (last name of the customer)

Email (email address of the customer)

Phone (phone number of the customer)

To delete a customer from the "Customers" table based on a specific condition, we can use the DELETE statement:

DELETE FROM Customers
WHERE CustomerID = 1;

The DELETE FROM clause is followed by the name of the table from which you want to delete records. In this case, it's the "Customers" table.

The WHERE clause is optional but highly recommended to specify the condition for deleting specific rows. In this example, we delete the customer with CustomerID = 1.

By executing this query, the customer with CustomerID = 1 will be deleted from the "Customers" table.

Deleting All Rows:

If you want to delete all rows from a table, you can omit the WHERE clause. For example:

DELETE FROM Customers;

This query will delete all rows from the "Customers" table, effectively emptying the table.

Caution:

Be cautious when using the DELETE statement without a WHERE clause or with broad conditions, as it can delete a large number of records or even all records from the table. Always double-check your conditions before executing a DELETE statement to avoid unintentional data loss.