Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:33:58+00:00 2026-06-10T12:33:58+00:00

Titling SQL questions is hard! Feel free to change it if you can think

  • 0

Titling SQL questions is hard! Feel free to change it if you can think how to describe this better!

I have a pretty typical set up: 3 tables, users, groups, and group_members.

Here’s an SQL Fiddle: http://sqlfiddle.com/#!2/23712/1

What I want to know is which groups are which users in.

So, I’m running:

SELECT u.id, u.firstname, u.lastname,
GROUP_CONCAT(m.group_id) as groups
FROM group_members m, users u
WHERE m.user_id = u.id
GROUP BY id
ORDER BY u.lastname ASC

Which is cool, and shows me the users name and what groups they are in.

My problem is that users who aren’t in any groups don’t show up, as of course the WHERE bit doesn’t match them.

How can I also return the users who aren’t in any group? (In the SQL Fiddle above, I want another row for Zack Jones, showing that he is either in group 0, or NULL or something!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:33:59+00:00Added an answer on June 10, 2026 at 12:33 pm

    You will want to use a LEFT JOIN of your users table on the group_members:

    SELECT u.id, 
      u.firstname, 
      u.lastname, 
      GROUP_CONCAT(m.group_id) as groups 
    FROM users u
    LEFT JOIN group_members m 
      ON u.id = m.user_id
    GROUP BY u.id, u.firstname, 
      u.lastname
    ORDER BY u.lastname ASC
    

    see SQL Fiddle with Demo

    Or you can RIGHT JOIN group_members to users

    SELECT u.id, 
      u.firstname, 
      u.lastname, 
      GROUP_CONCAT(m.group_id) as groups 
    FROM group_members m
    RIGHT JOIN users u
      ON m.user_id = u.id 
    GROUP BY id, u.firstname, 
      u.lastname
    ORDER BY u.lastname ASC
    

    see SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Think: tiling my emacs window with eshells, a la xmonad. Is this possible? I
I have this chunk of code. It works, but I'm sure there is a
I have a WPF canvas that I can scale, scroll and whatever. Now I
Had a really difficult time titling this one - please adjust as you see
I have a multiple unordered lists, with a h2 titling each list by subject.
I have a gradient image as a background for a table header(9x18pix). This image
I have a tiling background image, but I want to overlay another tiling image
I have to do application for tilling in C#. The tiles will have some
I have had the problem described in the question Tiling rectangles seamlessly in WPF
I know this probably isn't possible, but I would like to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.