How to delete the records from a table by using inner join and without using "in" condition?

Delete the records from a table using a select query with inner join is quite simple.

Let's create two tables as "TableA" and "TableB" respectively

TableA
Create table TableA
(
Id int,
Name varchar(20)
)
Go

TableB
Create table TableB
(
Id int,
Name varchar(20)
)

Go

Insert few records in both tables with some common Ids -

Insert into TableA
Insert into TableA(Id,Name)values(1,'AAA'),(2,'BBB'),(3,'CCC'),(4,'DDD'),(5,'EEE')
Go

Insert into TableB
Insert into TableB(Id,Name)values(1,'AAA'),(2,'BBB'),(6,'FFF'),(7,'GGG'),(8,'HHH') 
Go

Apply inner join between both tables to fetch the records with common Ids-
Select *from TableA inner join TableB on TableA.Id=TableB.Id

Result
Apply delete on join query
Delete TableA
from TableA inner join TableB on TableA.Id=TableB.Id

This will help you to delete the records from a table using select query with inner join.





Post a Comment

0 Comments