Search code examples
mysqlsqljoinforeign-key-relationshipsql-delete

What's the difference between INNER and LEFT join in DELETE statement?


When doing a delete I can zap rows in two or more tables by doing a LEFT JOIN.

DELETE a, b FROM
table1 a
LEFT JOIN table2 b on (a.id = b.id)
WHERE ...

I can also do an INNER JOIN

DELETE a, b FROM
table1 a
INNER JOIN table2 b on (a.id = b.id)
WHERE ...

What exactly is the difference between these two?

And what about other joins
Does it make sense to use other join types in a delete statement and how would these be useful?

What's the difference between a delete ... join and using a foreign key
If I set a foreign key with a ON DELETE CASCADE clause:

  1. Does that match an INNER or 'LEFT' join (or none of the above)?
  2. Is that somehow 'better' (faster/more secure) than using a join?

I'm using MySQL, but I'd also like to know which other databases support it.


Solution

  • The difference is that the inner join won't delete anything that doesn't join. The left join can delete things that don't join depending on what is in your WHERE clause.