i have Multiple Role ID For Single Emplyee with different rows. I want to check each roleid exist or not in another table with different rows
Loading
i have Multiple Role ID For Single Emplyee with different rows. I want to check each roleid exist or not in another table with different rows
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Aug 14, 2023, 10:26 AM EDT
Assuming you have the following two tables:
Table
EmployeeRoles:Table
AvailableRoles:You want to check if each RoleID associated with an EmployeeID in the
EmployeeRolestable exists in theAvailableRolestable.Here's a SQL query that can help you achieve this:
SELECT ER.EmployeeID, ER.RoleID, CASE WHEN AR.RoleID IS NOT NULL THEN 'Exists' ELSE 'Not Exists' END AS RoleStatus FROM EmployeeRoles ER LEFT JOIN AvailableRoles AR ON ER.RoleID = AR.RoleID;This query uses a LEFT JOIN between the
EmployeeRolesandAvailableRolestables on theRoleIDcolumn. It selects theEmployeeIDandRoleIDcolumns from theEmployeeRolestable and checks whether a match is found in theAvailableRolestable. If a match is found (RoleID exists), the RoleStatus column will show "Exists"; otherwise, it will show "Not Exists".Keep in mind that the above query assumes that you want to check all role IDs for each employee. If you want to check only specific role IDs or employees, you can modify the query accordingly using WHERE clauses.
Please replace table and column names with your actual table and column names if they are different in your database.
Tasadduq BurneyRank #1,427 · 1.1K reputationPosted Aug 15, 2023, 3:45 AM EDT
SELECT DISTINCT ER.EmployeeID, ER.RoleID, CASE WHEN RR.RoleID IS NOT NULL THEN 'Exists' ELSE 'Does Not Exist' END AS Status FROM EmployeeRoles ER LEFT JOIN RoleReference RR ON ER.RoleID = RR.RoleID;Abhishek YadavRank #217 · 11.3K reputationPosted Aug 16, 2023, 1:13 AM EDT
SELECT EmpID, RoleID, EXISTS ( SELECT 1 FROM [AnotherTable] WHERE RoleID = [ThisTable].RoleID ) AS RoleIDExists FROM [ThisTable]Cr BhargaviRank #420 · 5.9K reputationPosted Aug 14, 2023, 11:25 PM EDT
SELECT DISTINCT EmployeeID FROM EmployeeRolesTable e WHERE EXISTS ( SELECT 1 FROM OtherTable o WHERE o.RoleID = e.RoleID )Amit MohantyRank #112 · 20K reputationPosted Aug 16, 2023, 1:44 AM EDT
SELECT ER.EmployeeID, ER.RoleID, CASE WHEN R.RoleID IS NOT NULL THEN 'Exists' ELSE 'Does Not Exist' END AS RoleStatus FROM EmployeeRoles ER LEFT JOIN Roles R ON ER.RoleID = R.RoleID WHERE ER.EmployeeID = ;