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

  • Home
  • SEARCH
  • 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 9162397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:12:12+00:00 2026-06-17T14:12:12+00:00

This is probably very simple, but for some reason I am pulling a blank

  • 0

This is probably very simple, but for some reason I am pulling a blank on this one..

I want to join two tables and get the total occurrences of a column in one table within another table.
Table1

id | company
------------
 1 | companyA
 2 | companyB
 3 | companyC


Table2

id | company_id
------------
 1 |  2
 2 |  2
 3 |  1
 4 |  2
 5 |  3

The result should be:

company | count(*)
------------------
companyA| 1
companyB| 3
companyC| 1

I can’t seem to get the count portion correct. I was thinking that it would be something like:

SELECT Table1.company, count(*)
FROM Table1 JOIN Table2 ON
Table1.id = Table2.company_id
GROUP BY Table1.company;

Revised: The problem is actually that companyA and companyB come up correctly, but it’s not showing companyC. I went through and double checked that there are matches in the fields for companyC which there were.

  • 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-17T14:12:13+00:00Added an answer on June 17, 2026 at 2:12 pm

    it should be Table2.company_id that you are joining with and not Table2.id

    SELECT  Table1.company, count(*)
    FROM    Table1 
            INNER JOIN Table2 
               ON Table1.id = Table2.company_id  -- <<== here
    GROUP   BY Table1.company;
    

    you must define an INDEX (probably UNIQUE) on column company of table for faster performance since you are grouping it with company

    UPDATE 1

    SELECT  Table1.company, count(Table2.company_id)
    FROM    Table1 
            LEFT JOIN Table2 
               ON Table1.id = Table2.company_id  -- <<== here
    GROUP   BY Table1.company;
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this one is probably very simple, but I'm having a bit of trouble
This is probably very easy to do but for some reason I can't seem
This is probably a very simple question for some, but it has me stumped.
This is probably a very simple question for some, but it has me stumped.
This will probably require some looking into, but my question is very simple: Why
This is probably very simple but it's really confusing me. When I implement the
This is probably a very simple question but I'm a little confused how this
I know this is probably a very simple question but how would I do
This is another probably very simple question, but I haven't been able to find
This is probably a very simple question so please forgive my ignorance, but can

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.