I have following two tables ‘USERS’ and ‘GROUPS’:
USERS
-id
-name
-groupid
GROUP
-id
-name
I’d like to return all users along with their group’s name and group id. It should be an outer join on group id field correct?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use a
LEFT JOINbetweenusersandgroupsso that users who are not in a group still show up in the result set, but with group name and id NULL:Side note: Ensure that you’re encasing the table name
groupin backticks because it is a reserved keyword.The result-set should look something like:
Changing
LEFTtoINNERin the above query would INNER JOIN the two tables and exclude the user “NotInGroup” from the result-set.