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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:30:32+00:00 2026-05-24T07:30:32+00:00

I have a table with 3 columns being country , user_id , gender .

  • 0

I have a table with 3 columns being country, user_id, gender.
The user_id just increments, there are a number of different countries and the gender is either male or female. Similar to this

user_id  country  gender
1        japan    male
2        peru     female
3        japan    female
4        fiji     male
5        peru     female

I want to run one query to return a unique list of countries, the total number of user_id‘s for the country and the male and female numbers for each country. so the results would look like:

country total male female
peru    2     NUL  2
japan   2     1    1
fiji    1     1    NUL

I have tried a number of differnet queries but can’t return all the results in one query and don’t want to use 2 queries if possible.

this is my last try – I know its a mess but I was trying anything:

SELECT DISTINCT a.country AS country, COUNT(b.gender) AS male, COUNT(c.gender) AS     female, COUNT(a.user_id) AS total
  FROM userattributes as a
  LEFT JOIN userattributes as b ON a.user_id=b.user_id
  LEFT JOIN userattributes as c ON a.user_id=c.user_id
  WHERE b.gender='male' AND c.gender='female'
  GROUP BY country;

Can anyone do the above in one SELECT?

thx

  • 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-24T07:30:35+00:00Added an answer on May 24, 2026 at 7:30 am

    There’s no need to JOIN the table with itself. Using a CASE expression with the SUM aggregate would suffice.

    SELECT  country
            , COUNT(*) AS total
            , SUM(CASE WHEN gender = 'male' THEN 1 ELSE 0 END) AS male
            , SUM(CASE WHEN gender = 'female' THEN 1 ELSE 0 END) AS female
    FROM    userattributes
    GROUP BY
            country
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with columns ID(int), Number(decimal), and Date(int only timestamp). There are
I have a table of 3-4 columns, the central one being task names that
I have a table with the only relevant column being completionDate, a number of
I have a table in SQL Server 2005 with a variable number of columns,
I have a table in my database containing football results, the relevant columns being
In SQLite I have a table of contacts with one of the columns being
Let's assume that I have a table with two columns A and B. There
I have a table with 5 columns :- Country Code Language Warehouse ActiveFrom ActiveTo
I have a table with just 400-500 rows but this table is being accessed
I have an HTML table with one column being email addresses; spambot issues aside,

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.