SQL DELETE Query | Delete Existing Records in Table

The DELETE Query in SQL deletes existing records in the table. You can delete a single as well as multiple records according to the condition. Also, we can specify which rows to delete by using the WHERE Condition. In this article, we will discuss the SQL DELETE Query. Also, we will discuss a few examples of writing it.

Syntax

DELETE FROM table_name WHERE condition;

  • table_name: Specify the table name from which to delete records.
  • condition: The WHERE Condition to determine which rows to delete.

Note: If you don’t mention the WHERE condition, then the DELETE Query will delete all the rows of the table in SQL. Therefore, you should always mention the condition if you need to delete only some particular rows.

Demo Table

For instance, consider the demo table to refer to all the queries in this article.

mysql> SELECT * FROM employees;
+----+--------+--------+------------+-------+
| id | name   | salary | experience | team  |
+----+--------+--------+------------+-------+
|  1 | David  |  20000 |          5 | Alpha |
|  2 | Monica |  25000 |          6 | Beta  |
|  3 | John   |  15000 |          3 | Beta  |
|  4 | Mary   |  18000 |          4 | Gamma |
|  5 | Ross   |  30000 |          8 | Alpha |
+----+--------+--------+------------+-------+

Examples

Let’s discuss a few examples of using the query.

Example 1: Delete One Row From Table

For example, let’s delete one row from our demo table.

mysql> DELETE FROM employees WHERE name = 'Ross';
Query OK, 1 row affected (0.07 sec)

The above DELETE query in SQL deletes the row with name Ross. It only deletes one row. The remaining entries in the are:

+----+--------+--------+------------+-------+
| id | name   | salary | experience | team  |
+----+--------+--------+------------+-------+
|  1 | David  |  20000 |          5 | Alpha |
|  2 | Monica |  25000 |          6 | Beta  |
|  3 | John   |  15000 |          3 | Beta  |
|  4 | Mary   |  18000 |          4 | Gamma |
+----+--------+--------+------------+-------+

Example 2: Deleting Multiple Rows From Table

Likewise, if multiple rows satisfy the condition, the query will also delete them.

mysql> DELETE FROM employees WHERE salary >= 20000;
Query OK, 2 row affected (0.07 sec)

The above query deletes two rows from the table.

Example 3: Deleting All Rows From Table

Also, if you don’t specify the condition to delete rows, the DELETE Query in SQL will delete all the rows from the table. So, always remember to specify a condition when you want to delete only some specific rows.

mysql>DELETE FROM employees;

The query will delete all the rows from the table.

mysql> SELECT * FROM employees;
Empty set (0.00 sec)

Conclusion

In conclusion, we discussed the SQL DELETE Query in this article. You can read more about it on Wikipedia. Additionally, you can also read more articles on SQL on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *