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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:00:25+00:00 2026-06-09T13:00:25+00:00

I have this tables: A: id 1 2 B: id a_id 1 1 2

  • 0

I have this tables:

A:
id
1
2

B:
id a_id
1  1
2  1
3  1

C:
id a_id
1  1
2  1
3  2

I need this result:

A, CountB, CountC
1,      3,      2
2,      0,      1

This try doesnt work fine:

SELECT 
    A.id, COUNT(B.id), COUNT(C.id)
FROM
    A
        LEFT JOIN
    B ON A.id = B.a_id
        LEFT JOIN
    C ON A.id = C.a_id
GROUP BY A.id

How must be the sql sentence without using correlative queries?

  • 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-09T13:00:26+00:00Added an answer on June 9, 2026 at 1:00 pm

    The following variation on yours should work:

    SELECT A.id, COUNT(distinct B.id), COUNT(distinct C.id)
    FROM A LEFT JOIN
         B
         ON A.id = B.a_id LEFT JOIN
         C
         ON A.id = C.a_id
    GROUP BY A.id 
    

    However, there are those (such as myself) who feel that using count distinct is a cop-out. The problem is that the rows from B and from C are interfering with each other, multiplying in the join. So, you can also do each join independently, and then put the results together:

    select ab.id, cntB, cntC
    from (select a.id, count(*) as cntB
          from A left outer join
               B
               on A.id = B.a_id
          group by a.id
         ) ab join
         (select a.id, count(*) as cntC
          from A left outer join
               C
               on A.id = C.a_id
          group by a.id
         ) ac
         on ab.id = ac.id
    

    For just counting, the first form is fine. If you need to do other summarizations (say, summing a value), then you generally need to split into the component queries.

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

Sidebar

Related Questions

I have this query: SELECT COUNT(*) AS `numrows` FROM (`tbl_A`) JOIN `tbl_B` ON `tbl_A`.`B_id`
i have some complex select and result need to place in the table. I
I Have a query SELECT classid, COUNT(*) as cnt FROM tbl_name GROUP BY classid
I have this two tables: Table A ID TYPE SDATE EDATE RATING 1 M
I have a couple of tables which look like this Table 1 user_id |
I have (again) this tables: User: id | name ------------- 1 | 'John' 2
today i need your help to get an specific sql select query. i have
So I have this function that gets data from database and echoes it. The
I have a table where i store all product orders. I need to count
SELECT v.*, COUNT(a.*) FROM vacancies AS v, applications AS a WHERE a.vacancy_id = v.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.