SQL Cookbook Lesson
How to delete a row in SQL
Learn how to delete a row in SQL with examples and explanations from SQLPad.
Problem
You have a table in your SQL database and you want to delete a specific row from this table.
Sample Data
Consider a simple table called employees, which has the following data:
| id | first_name | last_name | |
|---|---|---|---|
| 1 | John | Doe | [email protected] |
| 2 | Jane | Doe | [email protected] |
| 3 | Jim | Brown | [email protected] |
MySQL Solution
DELETE FROM employees WHERE id = 3;
Explanation for MySQL Solution
In MySQL, the DELETE command is used to remove rows from a table. The WHERE clause is used to specify the conditions that must be met for a row to be deleted. In our example, the row with an id of 3 will be removed from the employees table.
PostgreSQL Solution
DELETE FROM employees WHERE id = 3;
Explanation for PostgreSQL Solution
The way to delete a row in PostgreSQL is the same as in MySQL. The DELETE command is used, along with the WHERE clause to specify the condition that must be satisfied for a row to be deleted. In this case, the row with an id of 3 in the employees table will be deleted.