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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:52:51+00:00 2026-06-07T00:52:51+00:00

I have a seemly simple question with SQL that I am having trouble figuring

  • 0

I have a seemly simple question with SQL that I am having trouble figuring out. Suppose I have the following tables:

company: 
    company_id
    company_title

users: 
    user_id
    username

company_owners: 
    company_id
    user_id

Now, there can be multiple users as company owners. Here is some example data:

company:
1, "A Company"
2, "B Company"
3, "C Company"

users:
1, "A User"
2, "B User"
3, "C User"

company_owners:
1,1
1,2
1,3
2,3

I am trying to create a query (MySQL) that gets the company_title, as well as the number of owners for that company, based on a specific company owner. So, for example:

Example query (in english) run: Get number of owners for each company that “C User” is an owner for:

company_id=1, company_title="A Company", num_owners=3
company_id=2, company_title="B Company", num_owners=1
company_id=3, company_title="C Company", num_owners=0

I have tried:

SELECT COUNT(user_id), company.* FROM `company` 
LEFT JOIN `company_owners` ON company_owners.company_id = company.company_id 
WHERE company_owners.user_id=1 GROUP BY company_id

But that always gives me an ownership number of “1”, I’m assuming because its only COUNTing the rows where user_id=1.

Does anyone have any ideas? I can supply more detail if need be.

Thanks so much!

  • 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-07T00:52:52+00:00Added an answer on June 7, 2026 at 12:52 am

    Your WHERE clause should be “where there exists an owner for this company with user_id = 1”.

    SELECT COUNT(user_id), company.*
    FROM `company` 
    LEFT JOIN `company_owners`
    ON company_owners.company_id = company.company_id 
    WHERE EXISTS
    (
        SELECT *
        FROM company_owners AS co2
        WHERE company_owners.company_id = co2.company_id
        AND co2.user_id = 3
    )
    GROUP BY company_id
    

    See it working online: sqlfiddle

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

Sidebar

Related Questions

So I have a problem that I can not figure out. I am writing
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a simple contact us XPage created. Have server side validation in place that
Have the following problem example . The problem is that because of the border
Have some text files that I need to break out the individual words. But
Have a painfully simple blog Post creator, and I'm trying to check if the
Have 2 tables in Access 2007, both lists of certain tasks to be accomplished.
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Have a rather abstract question for you all. I'm looking at getting involved in
Have been following Rails Tutorial by Michael Hart rails version 3.0 on mac OS

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.