Search code examples
sqlsql-serversql-delete

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:

  • Users table: ID and Active
  • Leaders table: ID

Solution

  • 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
                 )