How to Delete from a Table where rows match in Another Table – MySQL

0 min read 103 words

So you have a table where you want to delete a bunch of rows, based on a particular column being matched in another table.

This is easily achievable with MySQL.

DELETE FROM TABLE1
WHERE domain IN (
	SELECT domain FROM TABLE2
)

The above SQL will delete all rows in TABLE1 that are found in TABLE2.

But what if we need to limit which records are returned from TABLE2?

That’s simple too. All we need to do is add a traditional WHERE clause to our select statement.

DELETE FROM TABLE1
WHERE domain IN (
	SELECT domain FROM TABLE2
	WHERE column1='1' AND column2<>'2'
)
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags