How can I delete using INNER JOIN with SQL Server?

1,855,317

Solution 1

You need to specify what table you are deleting from. Here is a version with an alias:

DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
  ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND Date = '2013-05-06'

Solution 2

Just add the name of the table between DELETE and FROM from where you want to delete records, because we have to specify the table to delete. Also remove the ORDER BY clause because there is nothing to order while deleting records.

So your final query should be like this:

    DELETE WorkRecord2 
      FROM WorkRecord2 
INNER JOIN Employee 
        ON EmployeeRun=EmployeeNo
     WHERE Company = '1' 
       AND Date = '2013-05-06';

Solution 3

It is possible this will be helpful for you -

DELETE FROM dbo.WorkRecord2
WHERE EmployeeRun IN (
    SELECT e.EmployeeNo
    FROM dbo.Employee e
    WHERE ...
)

Or try this -

DELETE FROM dbo.WorkRecord2
WHERE EXISTS(
    SELECT 1
    FROM dbo.Employee e
    WHERE EmployeeRun = e.EmployeeNo
        AND ....
)

Solution 4

Try this:

DELETE FROM WorkRecord2 
       FROM Employee 
Where EmployeeRun=EmployeeNo
      And Company = '1' 
      AND Date = '2013-05-06'

Solution 5

It should be:

DELETE zpost 
FROM zpost 
INNER JOIN zcomment ON (zpost.zpostid = zcomment.zpostid)
WHERE zcomment.icomment = "first"       
Share:
1,855,317
nettoon493
Author by

nettoon493

Hi, EyeryBody I beginning ASP.NET Pleased to meet you. :))

Updated on October 04, 2021

Comments

  • nettoon493
    nettoon493 over 2 years

    I want to delete using INNER JOIN in SQL Server 2008.

    But I get this error:

    Msg 156, Level 15, State 1, Line 15
    Incorrect syntax near the keyword 'INNER'.

    My code:

    DELETE 
    FROM WorkRecord2 
    INNER JOIN Employee 
            ON EmployeeRun=EmployeeNo
    WHERE Company = '1' 
        AND Date = '2013-05-06'
    
    • Pondlife
      Pondlife almost 11 years
      Example C in the documentation shows how to use DELETE with a join
    • reggaeguitar
      reggaeguitar over 4 years
      Example C uses a cursor and a bunch of extraneous stuff too
    • fish-404
      fish-404 over 2 years
      Example D Using joins and subqueries to data in one table to delete rows in another table may be the correct one.
  • oabarca
    oabarca almost 10 years
    @bluefeet could you provide the right syntax for SQL Server for deleting from both tables?
  • Taryn
    Taryn almost 10 years
    @user2070775 In SQL Server to delete from 2 tables you need to use 2 separate statements.
  • Mathieu Rodic
    Mathieu Rodic almost 10 years
    @user2070775 in SQL Server, you can use transactions and pseudo-tables, as shown in stackoverflow.com/questions/783726/…
  • Stealth Rabbi
    Stealth Rabbi about 9 years
    I'm pretty sure a DELETE can only specify one table. This doesn't work for me.
  • Chris Moschini
    Chris Moschini over 8 years
    This is the only answer that works on Sql Server. Just build your query like select Id from... join ... join etc then wrap it as a subquery and do a delete from (table) where Id in (subquery)
  • Verena Haunschmid
    Verena Haunschmid over 7 years
    @MathieuRodic thanks for sharing. In my setup if I delete from the 2 tables separately I don't really know anymore which rows to delete from the 2nd table so this will help :)
  • Pradeep Kumar
    Pradeep Kumar over 7 years
    "DELETE FROM table1 INNER JOIN table2 ON xyz". Will this query delete from table1 alone or from both table1 and table2?
  • dandev91
    dandev91 over 7 years
    I believe you can specify multiple tables for deletion in mySQL, but not SQL Server (which the question asks).
  • stomy
    stomy about 6 years
    No need to mention target table more than once. See docs.microsoft.com example 3
  • Shahryar Saljoughi
    Shahryar Saljoughi about 6 years
    what is w in front of delete keyword?
  • Taryn
    Taryn about 6 years
    @ShahryarSaljoughi that is the alias for the WorkRecord2 table.
  • Darren Griffith
    Darren Griffith almost 6 years
    This question is for SQL Server. You cannot delete from two tables in one statement in SQL Server. My understanding is this can be done in mysql and MS Access.
  • TroySteven
    TroySteven over 5 years
    This one works on SQL Server if you only intend to delete from the first table.
  • Himanshu
    Himanshu over 5 years
    @matwonk: You can delete from second table to if you use second table's name. E.g. using DELETE Employee will delete from Employee's table instead of WorkRecord2 table.
  • Himanshu
    Himanshu about 5 years
    @matwonk: Here is an example: 1) Deleting from the first table 2) Deleting from the second table.
  • Prajakta Kale
    Prajakta Kale over 4 years
    what should be the query to delete data from both the tables at a time?
  • Geoff Griswald
    Geoff Griswald over 3 years
    Build 2 temp tables, containing the rows you want to delete from each table. Run two separate delete statements on each table, one at a time, and join to one of the temp tables each time.
  • Geoff Griswald
    Geoff Griswald over 3 years
    This is one of many answers that works on SQL Server. I suggest the accepted answer as the best way to do it.
  • Daniel
    Daniel about 3 years
    Deletion order can matter if you are deleting from a table that has itself as a foreign key (And not using cascading delete.) But I agree in general it doesn't matter...
  • NTDLS
    NTDLS over 2 years
    @user2070775 you are 100% right, but with cascade delete foreign keys, the same effect can be accomplished with a single user supplied statement.