Search code examples
sqlsql-servermssql-jdbc

SQL INNER JOIN + SELECT NOT EXIST ID's


I have three tables:

Table One: Users

Table Two: Roles

Table Three: UserInRoles

  • Users

UserID                                |  FullName
--------------------------------------------------
07DCEE4A-6598-42E1-95C6-0390FF8BB534  | John Doe
  • Roles

RoleID
---------------------------------------
E5C46F8E-EE6A-4052-AABA-08184E5F0158
  • UserInRoles

UserID                               | RoleID
---------------------------------------------------------------------------
07DCEE4A-6598-42E1-95C6-0390FF8BB534 | E5C46F8E-EE6A-4052-AABA-08184E5F0158

I need to Select all UsersID who's not in table UserInRoles from table Users

I tryed :

SELECT DISTINCT Users.UserId, Users.FullName 
FROM Users 
INNER JOIN UserInRoles 
ON Users.UserId <> UserInRoles.UserId

Solution

  • To Select all UsersID who's not in table UserInRoles from table Users, simply use not in

    select distinct * from users where userid not in
    (select userid from UserInroles)