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 9013305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:13:44+00:00 2026-06-16T03:13:44+00:00

I have a table users: user_id name 1 John 2 Dan 3 Jane 4

  • 0

I have a table users:

user_id    name
1          John
2          Dan
3          Jane
4          Sophie
5          Jodie

I then have a table named associates:

user_id    assoc_id
1          2
1          3 
3          4
3          1
3          5
4          1
5          1
5          2

What I want to do is show how many associates each user has, or none

So, the results would show

user_id    Name     Number of Associates
1          John     2
2          Dan      0
3          Jane     3
4          Sophie   1
5          Jodie    2

What I’m trying works but does not show those with 0

Here’s what I’m trying, how do I get the 0s?

SELECT u.user_id, u.name, count(a.user_id) as howmany from users u
join associates a on a.user_id = u.user_id
group by u.user_id order by u.user_id asc
  • 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-16T03:13:45+00:00Added an answer on June 16, 2026 at 3:13 am

    You need a LEFT OUTER JOIN:

    SELECT u.user_id, u.name, count(a.user_id) as howmany 
    FROM users u LEFT OUTER JOIN associates a 
    ON a.user_id = u.user_id
    GROUP BY u.user_id 
    ORDER BY u.user_id ASC
    

    The LEFT OUTER JOIN will include every rows of the first table even is they aren’t present in the joined table.

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

Sidebar

Related Questions

I have an InnoDB table containing users, like this: +--------------+-----------------------+ | user_id | name
I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many
I have two tables user table user_id | name | 1 | peter |
I have 3 tables: users (id, name) currency (id, name) accounts (id, user_id, currency_id,
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
I have a table for users: USERS: ID | NAME | ---------------- 1 |
I have a mysql table like below: id name points 1 john 4635 3
I have users user_id usr_name email 1 john john@yahoo.com 2 mike mike@gmail.com 3 tom
I have two tables Users: (id, name) Relations: (user_id, relation_id) User_id and relation_id are
I have to tables called USERS and COMM_HISTORY USERS(user_id, name) COMM_HISTORY(comm_history_id, comm_date, comm_by, user_id)

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.