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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:32:32+00:00 2026-05-16T01:32:32+00:00

I have two tables, foo and bar : +—-+—–+ +—-+—–+ | id | val

  • 0

I have two tables, foo and bar:

+----+-----+    +----+-----+
| id | val |    | id | val |
+----+-----+    +----+-----+
|  1 | qwe |    |  1 | asd |
|  2 | rty |    |  3 | fgh |
+----+-----+    +----+-----+

id is not unique here. Not all IDs in foo have their equivalents in bar and vice versa. I need to count all rows with specific ID in both tables and present them in a new table, e.g.:

+----+-----------+-----------+
| id | count_foo | count_bar |
+----+-----------+-----------+
|  1 |         1 |         1 |
|  2 |         1 |         0 |
|  3 |         0 |         1 |
+----+-----------+-----------+

I’ve tried UNION SELECT:

SELECT id, COUNT(id) AS count_foo, 0 AS count_bar FROM foo GROUP BY id
UNION SELECT id, 0, COUNT(id) FROM bar GROUP BY id;

But this outputs row with id=1 twice, like

+----+-----------+-----------+
| id | count_foo | count_bar |
+----+-----------+-----------+
|  1 |         1 |         0 |  <- not good
|  2 |         1 |         0 |
|  1 |         0 |         1 |  <- not good
|  3 |         0 |         1 |
+----+-----------+-----------+

I’ve also tried LEFT JOIN:

SELECT id, COUNT(foo.id) AS count_foo, COUNT(bar.id) AS count_bar
FROM foo LEFT JOIN bar USING(id) GROUP BY id;

But this query ignores rows from table bar with ID that is missing in table foo:

+----+-----------+-----------+
| id | count_foo | count_bar |
+----+-----------+-----------+
|  1 |         1 |         1 |
|  2 |         1 |         0 |
+----+-----------+-----------+  <- can I haz `id=3`?

What am I missing? What would be the right query or the right manual to read?

Thanks.

  • 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-05-16T01:32:33+00:00Added an answer on May 16, 2026 at 1:32 am

    You may want to try the following:

    SELECT d.id,
           (SELECT COUNT(*) FROM foo WHERE id = d.id) count_foo,
           (SELECT COUNT(*) FROM bar WHERE id = d.id) count_bar
    FROM   ((SELECT id FROM foo) UNION (SELECT id FROM bar)) d;
    

    Test case:

    CREATE TABLE foo (id int, val varchar(5));
    CREATE TABLE bar (id int, val varchar(5));
    
    INSERT INTO foo VALUES (1, 'qwe');
    INSERT INTO foo VALUES (2, 'rty');
    INSERT INTO bar VALUES (1, 'asf');
    INSERT INTO bar VALUES (3, 'ghj');
    

    Result:

    +------+-----------+-----------+
    | id   | count_foo | count_bar |
    +------+-----------+-----------+
    |    1 |         1 |         1 |
    |    2 |         1 |         0 |
    |    3 |         0 |         1 |
    +------+-----------+-----------+
    3 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer declare @sql nvarchar(max); set @sql = N'select * from table… May 16, 2026 at 4:41 am
  • Editorial Team
    Editorial Team added an answer You're on the right track: ...the DOM tree of a… May 16, 2026 at 4:41 am
  • Editorial Team
    Editorial Team added an answer The simplest way is to use an external diff tool… May 16, 2026 at 4:41 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.