
For an instructor lead, in-depth look at learning SQL click below.
In this blog post, you will be taken through the process of deleting a record from a table in SQL. SQL, which stands for Structured Query Language, provides a variety of commands to manipulate data stored in a database. One such command is the DELETE statement, which is used to remove existing records from a table.
Basic Syntax for DELETE
The basic syntax for the DELETE statement in SQL is:
|
1 2 3 4 |
DELETE FROM table_name WHERE some_column = some_value; |
This command removes rows of data from a table (“table_name”), based on the condition specified (“some_column = some_value”). The ‘WHERE’ clause is particularly important: if you leave it off, you’ll delete every row from the table.
Example of DELETE Statement
Let’s say we have a table ‘Employees’ with the following data:
|
1 2 3 4 5 6 7 8 9 10 |
SELECT * FROM Employees; EmployeeId | Name | Department ------------------------------- 1 | John Doe | HR 2 | Jane Smith| Engineering 3 | Max Rain | HR 4 | Emma Stone| Marketing |
And we want to delete the record of the employee with EmployeeId 4. We can achieve that by running the following DELETE statement:
|
1 2 3 4 |
DELETE FROM Employees WHERE EmployeeId = 4; |
If you now run the SELECT * FROM Employees command again, you would see that employee 4, Emma Stone, has been successfully deleted from our table.
Conclusion
While the DELETE statement is quite useful, it is very powerful and can cause damage to your data if not used carefully. Always ensure that you have properly defined the WHERE condition to avoid deleting wrong or excessive data. Happy Coding!
|
1 2 |
