I want to delete a row if a particular x value matches a y value in any row in the same table.
Ex:
| x | y | | 4 | 2 | | 2 | 6 | | 8 | 1 | | 3 | 1 | | 7 | 8 | | 9 | 5 | would become:
| x | y | | 4 | 2 | | 3 | 1 | | 7 | 8 | | 9 | 5 | I want to delete a row if a particular x value matches a y value in any row in the same table.
Ex:
| x | y | | 4 | 2 | | 2 | 6 | | 8 | 1 | | 3 | 1 | | 7 | 8 | | 9 | 5 | would become:
| x | y | | 4 | 2 | | 3 | 1 | | 7 | 8 | | 9 | 5 | If your DB allows it, a self-join may work:
DELETE FROM foo AS xside LEFT JOIN foo AS yside ON xside.y = yside.x LEFT JOIN (subquery) AS ysideDelete from tab where x in ( select y from tab) Alternate version to counter null values in y column.
Delete from tab t where exists ( select 1 from tab ta where ta.y = t.x) NULL issue is, in my opinion, the strongest reason to avoid IN. Some RDBMS optimise better than others, but all will bite you int he list is NULLable.