# What SQL query can I use to delete values in one sql table that match those in another table?

**URL:** <https://forum.inductiveautomation.com/t/what-sql-query-can-i-use-to-delete-values-in-one-sql-table-that-match-those-in-another-table/76593>\
**Category:** Ignition\
**Created:** [July 19, 2023, 4:12pm UTC](https://forum.inductiveautomation.com/t/what-sql-query-can-i-use-to-delete-values-in-one-sql-table-that-match-those-in-another-table/76593 "2023-07-19T16:12:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [July 19, 2023, 4:12pm UTC](https://forum.inductiveautomation.com/t/what-sql-query-can-i-use-to-delete-values-in-one-sql-table-that-match-those-in-another-table/76593/1 "2023-07-19T16:12:01Z")

</div>

I have tables A and B and I want to delete any value in table A that is also in table B.  
Table A has [1, 2, 3, 4, 5, 6] and B has [2, 4, 5].

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 19, 2023, 4:13pm UTC](https://forum.inductiveautomation.com/t/what-sql-query-can-i-use-to-delete-values-in-one-sql-table-that-match-those-in-another-table/76593/2 "2023-07-19T16:13:32Z")

</div>

You actually want to delete those values, or you just wan't to do a Join where only values that exist in both tables are selected?

The syntax will be realtively similare either way.

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [July 19, 2023, 4:22pm UTC](https://forum.inductiveautomation.com/t/what-sql-query-can-i-use-to-delete-values-in-one-sql-table-that-match-those-in-another-table/76593/3 "2023-07-19T16:22:46Z")

</div>

delete B values from A

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 19, 2023, 4:31pm UTC](https://forum.inductiveautomation.com/t/what-sql-query-can-i-use-to-delete-values-in-one-sql-table-that-match-those-in-another-table/76593/4 "2023-07-19T16:31:21Z")

</div>

I am assuming that your data is a bit more complex then the sample that you have provided.

The query would look something like:

```auto
DELETE
FROM [Table A]
WHERE Some_ID_Column IN (Select Some_ID_Column FROM [Table B])

```

I would recommend that you run a select statement first to insure you're syntax actually properly selects the rows you are actually wanting to delete.

```auto
Select *
FROM [Table A]
WHERE Some_ID_Column IN (Select Some_ID_Column FROM [Table B])

```

The ID column doesn't need to actually be an ID column, but some identifying expression that selects single rows from Table A.

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [July 19, 2023, 4:58pm UTC](https://forum.inductiveautomation.com/t/what-sql-query-can-i-use-to-delete-values-in-one-sql-table-that-match-those-in-another-table/76593/5 "2023-07-19T16:58:52Z")

</div>

It works, thank you!
