Ahmed Hassan Ahmed Hassan - 4 months ago 7
SQL Question

SQL query condition to select specific records on a particular table

I have three tables

Users ( ID - Name )
Services (ID - Name)
UserServices ( ID - USerID- ServiceID )


I want to select serviceName that not belong to user

eg :
if i have three users and 10 services

First user belong to 3 services so i want to select other 7 services only

Answer
SELECT S.ID, S.Name
FROM Services AS S 
LEFT JOIN UserServices AS US
ON US.ServiceID = S.ID
AND US.UserID = @UserID
WHERE US.ServiceID IS NULL

So, select the services where there are no matching UserServices for that User Id. I also changed your US.ID to US.UserID, change it back if US.ID actually does contain your UserIDs.