• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

ASP.NET query to delete

I need a query that deletes everything from the "Names" table but only where where is not a matching ID in the "Departments" table.  So in the example below, ID's 1, 3 and 6 would  be deleted, but ID's 2, 4 and 5 would not be deleted.  I can't figure out the query.

   Names                                                    Departments

ID     Name                                           ID            Department
==     =====                                          ==            ==========
1       Smith                                          2               Accounting
2       Jones                                           4               Management
3       Miller                                          5               Purchasing
4       Washington
5       Lewis
6       Brown
0
zintech
Asked:
zintech
1 Solution
 
AnujSQL Server DBACommented:
DELETE FROM Name
FROM Names N
INNER JOIN Department M
ON M.ID = N.ID
WHERE N.ID IS NULL
0
 
hafeezmcaCommented:
Hi try this:

delete from names where Id not in (select e.Id from Names e, Departmentt d where e.Id=d.Id)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now