how to DELETE from a table in SQL Server using an INNER JOIN

All topics about programming / development in T-SQL or other languages for SQL Server.
Post Reply
devILC
Posts: 2
Joined: Mon Jul 22, 2019 5:55 pm

Mon Jul 22, 2019 5:58 pm

Hello,

I'm new to SQL Server, can anybody tell me how i can DELETE from a table using an INNER JOIN?

I keep getting the below error:

Code: Select all

Incorrect syntax near the keyword 'INNER'.
Here is my SQL Code:

Code: Select all

DELETE FROM dbo.Students S
INNER JOIN dbo.Address A ON A.StudentID = S.ID
WHERE A.City = 'Liverpool'
Please can anybody help?
by DaveP5 » Mon Jul 22, 2019 6:01 pm
Hi @devILC

You need to specify which table you are deleting from.

The below should work (If you are trying to DELETE the students, otherwise change the code to DELETE A):

Code: Select all

DELETE S
FROM dbo.Students S
INNER JOIN  dbo.Address A ON A.StudentID = S.ID
WHERE A.City = 'Liverpool'
Go to full post
DaveP5
Posts: 29
Joined: Sat Jun 22, 2019 7:58 pm
Answers: 1

Mon Jul 22, 2019 6:01 pm

Hi @devILC

You need to specify which table you are deleting from.

The below should work (If you are trying to DELETE the students, otherwise change the code to DELETE A):

Code: Select all

DELETE S
FROM dbo.Students S
INNER JOIN  dbo.Address A ON A.StudentID = S.ID
WHERE A.City = 'Liverpool'
devILC
Posts: 2
Joined: Mon Jul 22, 2019 5:55 pm

Mon Jul 22, 2019 6:01 pm

Thanks @DaveP5
Post Reply

Social Media