Solved

ASP.NET query to delete

Posted on 2012-04-10
2
343 Views
Last Modified: 2012-06-22
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
Comment
Question by:zintech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Expert Comment

by:Anuj
ID: 37828225
DELETE FROM Name
FROM Names N
INNER JOIN Department M
ON M.ID = N.ID
WHERE N.ID IS NULL
0
 
LVL 5

Accepted Solution

by:
hafeezmca earned 500 total points
ID: 37828340
Hi try this:

delete from names where Id not in (select e.Id from Names e, Departmentt d where e.Id=d.Id)
0

Featured Post

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question