user10018802
user10018802

Reputation:

SQL Server : how to delete based on another table

I have a USERS table with active and inactive users and I also have another table called Leaders where team leaders are stored (so a list of users).

I want to delete those users in table Leaders that are inactive in table users.

Edit based on comments:

Upvotes: 2

Views: 82

Answers (2)

Felipe Martins
Felipe Martins

Reputation: 184

You can make joins in delete, similar to select:

delete ld
from leaders ld
join users us on ld.idUser = us.idUser
where us.active = 0

Upvotes: 2

Mureinik
Mureinik

Reputation: 311028

You could use an in condition:

DELETE
FROM   leaders
WHERE  id IN (SELECT id
              FROM   users
              WHERE  active = 0 -- Or however you mark inactive users
             )

Upvotes: 3

Related Questions