Home » Tutorials » SQL » DELETE Query

DELETE Query

Now, we meet the most dangerous SQL statement, DELETE. DELETE removes existing records from tables. Many nightmares have occurred because of the DELETE statement. I strongly recommend testing your DELETE statements in another test database before you run the command in the real database. SQL does not provide some magical way to recover deleted data because that is your job as a web developer. Programming and database languages are super smart, but they can’t save you from everything. Enough of the warnings, you will learn on your own… Example please:

Deleting Records in SQL

Our table before:

idusernamepasswordbirthday
1bobdole32secretP1984-06-01
2rustyMeerkatdigholes1995-09-15
Example
DELETE FROM table_name
WHERE username = ‘rustyMeerkat’
Result
idusernamepasswordbirthday
1bobdole32secretP1984-06-01

It’s pretty easy to understand; however, I need to stress Don’t leave off the WHERE. If you leave off WHERE, you will remove all of the records. The DELETE FROM is the standard introduction into the statement that SQL understands. As used before,WHERE is the conditional. Deleting isn’t very difficult, but please be careful. It is good practice to slowly build up your delete queries and quickly changing your DELETE statement to a SELECT statement to see what kind of data you will be deleting. After you see that the SELECT statement returned what you want to delete, you can finally switch it back to a DELETE statement.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "DELETE Query". After Hours Programming. Accessed on May 20, 2022. https://www.afterhoursprogramming.com/tutorial/sql/delete-query/.

  • Stewart, Suzy. "DELETE Query". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/sql/delete-query/. Accessed 20 May, 2022.

  • Stewart, Suzy. DELETE Query. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/sql/delete-query/.



0 thoughts on “DELETE Query”

  1. I have been involved a little bit in queries DBs. But here I read a very good advise (never thought about it) – first to build select query and after it returned expected result – replace “select” with “delete”. It is not that I never did it – I just never put my thought into it from this angle.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.