Friday, June 20, 2025
HomeJavaEasy methods to delete or take away rows from desk utilizing JOINs...

Easy methods to delete or take away rows from desk utilizing JOINs in SQL Server? Instance Tutorial


Whats up guys, there are sometimes a situation the place it’s essential to delete information from a desk through the use of joins. For instance, you wish to delete all employes who’re additionally supervisor on this case, it’s essential to self be part of with the desk to seek out all the workers who’re supervisor after which it’s essential to delete them. Equally, if it’s essential to delete all staff who should not assigned any division then it’s essential to use left or proper out be part of to seek out them after which delete it. Backside line is that It is a bit bit tough to delete from a desk whereas utilizing any sort of JOIN in SQL like Interior Be a part of, Left Outer Be a part of, or Proper Outer Be a part of. The apparent syntax does not work as proven beneath:

delete from #Expired e INNER JOIN 
Offers d ON e.DealId = d.DealId
The place d.Model = 'Sony'

right here I’ve a desk with a listing of expired offers that I wish to delete from the Offers tables, however just for Sony.

After I run this SQL Question in Microsoft SQL Server database, it gave me the next error:

Msg 102, Stage 15, State 1, Line 1
Incorrect syntax close to ‘e’.

Now, I’m puzzled, tips on how to delete from a desk whereas utilizing INNER JOIN in SQL Server?

Effectively, it turned out, I wasn’t utilizing the appropriate syntax. The DELETE clause must know the desk from which information must be deleted.

You may remedy the issue through the use of the desk alias as proven beneath:

delete e from #Expired e INNER JOIN Offers d ON e.DealId = d.DealId
The place d.Model = 'Sony'

This tells the SQL Server that deletes the matching row from the desk with alias “e” i.e. the #Expired desk. In case you are not a fan of utilizing desk aliases then it’s also possible to give the total desk identify there as proven beneath, this can even work effective within the Microsoft SQL Server database.

delete #Expired from #Expired INNER JOIN Offers ON #Expired.DealId = Offers.DealId
The place Offers.Model = 'Sony'

The identical syntax will work effective even in the event you use the Left Outer Be a part of or Proper Outer Be a part of.

SQL is like coding, each are key expertise that require every day apply. If you happen to do not write SQL queries or a few months, you’ll solely bear in mind the choose * from desk. Extra advanced queries involving becoming a member of greater than two tables in a single question can be obscure and write.

On the similar time, you do not get the chance to put in writing huge, advanced SQL queries every day, it is solely when it’s essential to generate stories, troubleshooting one thing, you want this ability.

Deleting From Desk Utilizing Be a part of – SQL Instance

Now, let’s examine a stay instance of tips on how to delete information from a desk utilizing Take part SQL Server. As a way to present one thing, we first have to create a Deal and Expired desk. You should utilize the next SQL question to create these two tables.

I’ve purposefully created them as a momentary desk, you possibly can see the prefix # as a result of they’re for demonstration solely. You also needs to not overlook to drop them as soon as you might be performed. A great SQL apply to comply with.

-- making a Offers desk 
Create desk #Offers (DealId BIGINT, Model VARCHAR(100), ExpiryDate DATE);
Insert into #Offers values (10001, 'Sony', '20160102');
Insert into #Offers values (10002, 'Samsung', '20160103');
Insert into #Offers values (10003, 'HP', '20160104');
Insert into #Offers values (10004, 'Intel', '20160105');
Insert into #Offers values (10005, 'Citibank', '20160106');
Insert into #Offers values (10003, 'HSBC', '20160107');
Insert into #Offers values (10004, 'Disney', '20160108');
Insert into #Offers values (10005, 'Motorola', '20160109');

Choose * from #Offers;

-- copying expired offers right into a briefly expired desk
Choose * into #Expired from #Offers the place ExpiryDate < '20160106'
Choose * From #Expired

-- cleanup
Drop desk #Offers;
Drop desk #Expired;

and right here is the output of those SQL queries once you run them on Microsoft SQL Server 2014:

How to delete from table using INNER Join in SQL

Now, let’s use the interior be part of between each #Offers and #Expired to take away Expired entries for model Sony from the #Expired desk.

How to delete from table while using SQL

You may see that the primary row with DealId=10001 has been deleted from the #Expired desk as a result of that is the row with the Sony model. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments