Skip to main content

Command Palette

Search for a command to run...

How to use the SQL DELETE clause

Published
1 min read
How to use the SQL DELETE clause
O

Hi there 👋🏾. I'm a software engineer that enjoys building stuff and talking about them. I also tinker a bit with hardware and robotics using Arduino and ROS.

Deletion is a fundamental operation when working with relational databases. It is used when one or more rows are to be removed from a table. The WHERE clause always accompanies the DELETE clause.

So, let's say we have a table of user posts. To delete rows without a user_id foreign key, we can run this query:

DELETE FROM `posts` WHERE user_id IS NULL;

You may be tempted to write it this way:

DELETE FROM `posts` WHERE user_id=NULL;

This doesn't work because NULL does not equal NULL in SQL. Since the value in the column is unknown, we can't compare it with another unknown. Check this StackOverflow page to learn more.

Thanks for reading. Adios ✌🏾🧡.

More from this blog

O

Osinachi's base

66 posts

Hi there, I'm a software engineer that enjoys building stuff and talking about them. I also tinker a bit with hardware and robotics using Arduino and ROS.