EN
MySQL - delete row where column is null
6 points
Quick solution:
xxxxxxxxxx
1
DELETE FROM `table_name` WHERE `column_name` IS NULL;
Sometimes your data can be an empty string. You can delete such rows with below query:
xxxxxxxxxx
1
DELETE FROM `table_name` WHERE `column_name` = '';
To show how to delete rows with NULL values, we will use the following table:
![MySQL - delete rows with NULL values - example data](/static/bucket/1618341966295-ZmODp9gANa--image.png)
Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will delete rows from users
table with NULL
value in email
column.
Query:
xxxxxxxxxx
1
DELETE FROM `users` WHERE `email` IS NULL;
Result:
![MySQL - delete rows with NULL column values - result](https://dirask.com/static/bucket/1618341377316-qxgDEjWAWo--image.png)
create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE `users` (
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`name` VARCHAR(50) NOT NULL,
4
`surname` VARCHAR(50) NOT NULL,
5
`email` VARCHAR(50),
6
PRIMARY KEY (`id`)
7
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO `users`
2
( `name`, `surname`, `email`)
3
VALUES
4
('John', 'Stewart', 'john@email.com'),
5
('Chris', 'Brown', NULL),
6
('Kate', 'Lewis', NULL),
7
('Ailisa', 'Gomez', 'ailisa@email.com'),
8
('Gwendolyn', 'James', NULL),
9
('Simon', 'Collins', NULL),
10
('Taylor', 'Martin', NULL),
11
('Andrew', 'Thompson', 'andrew123@email.com');