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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:21:43+00:00 2026-06-03T00:21:43+00:00

DataBase : SQL Fiddle Query needed : To return the number of women and

  • 0

DataBase: SQL Fiddle

Query needed: To return the number of women and men of age 25-35 years for each Insurance Company.

My Progress:

CREATE VIEW MenInAge AS
SELECT p.pname,p.pid,p.cid
FROM Patient p
WHERE p.gender = 'm' and p.age between 25 and 35;

CREATE VIEW WomenInAge AS
SELECT p.pname,p.pid,p.cid
FROM Patient p
WHERE p.gender = 'f' and p.age between 25 and 35;

CREATE VIEW MenInAgeCount AS
SELECT m.cid, COUNT(m.pid) as c
FROM MenInAge m
GROUP BY m.cid;

CREATE VIEW WomenInAgeCount AS
SELECT w.cid, COUNT(w.pid) as c
FROM WomenInAge w
GROUP BY w.cid;

How do I show for every InsuranceCompany.cid the WomenInAgeCount.c and the MenInAgeCount.c columns?

  • 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-03T00:21:45+00:00Added an answer on June 3, 2026 at 12:21 am

    Explanation:

    You have to join the tables InsuranceCompanies and Patient using the LEFT OUTER JOIN by joining the records on cid column in both tables and also apply the filter to select only patients between age 25 and 35 (including those boundary values). The CASE statement simply checks whether the patient is male or female and computes two different columns by assigning values of 1 if the values match and 0 if the values don’t match. Finally you have to group the result by cname to fetch the count by insurance company name.

    Explanation about CASE:

    In the CASE expression, the query states WHEN gender field value is f assign the column female with the value 1. The value 1 is hard coded because it means the query found 1 row matching the gender=’f’ record and this also represent 1 person. You can also state ELSE 0 but it is implicit so not necessary to specify that. This CASE expression evaluates for every record in the query result. Finanlly, you will get all the rows with female column containing either 1 or 0. When you sum this column female, you will get the total number of females, the same logic goes for male column.

    With COALESCE:

    COALESCE here replaces any NULL values with the given value in the second parameter (here in this case zero).

    Click here to view the demo in SQL Fiddle.

    Script:

    SELECT              ic.cname
                    ,   COALESCE(SUM(CASE WHEN gender = 'f' THEN 1 END), 0) female
                    ,   COALESCE(SUM(CASE WHEN gender = 'm' THEN 1 END), 0) male 
    FROM                InsuranceCompanies ic
    LEFT OUTER JOIN     Patient p
    ON                  p.cid = ic.cid
    AND                 age BETWEEN 25 AND 35
    GROUP BY            ic.cname;
    

    Output:

    CNAME      FEMALE MALE
    ---------- ------ ----
    Clalit Inc    0    2
    Harel Inc     2    0
    

    Without COALESCE:

    Click here to view the demo in SQL Fiddle

    Script:

    SELECT              ic.cname
                    ,   SUM(CASE WHEN gender = 'f' THEN 1 END) female
                    ,   SUM(CASE WHEN gender = 'm' THEN 1 END) male 
    FROM                InsuranceCompanies ic
    LEFT OUTER JOIN     Patient p
    ON                  p.cid = ic.cid
    AND                 age BETWEEN 25 AND 35
    GROUP BY            ic.cname;
    

    Output:

    CNAME      FEMALE MALE
    ---------- ------ ----
    Clalit Inc  NULL   2
    Harel Inc     2   NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Find Insurance Company with the maximal number of members (patients). Thats the Database and
If I run a stored procedure in my database (SQL 2000) and return the
In a legacy database (SQL Server 2000), we have a clustered index that looks
Does performance of a database (SQL Server 2005) decrease if I shrink it? What
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have one field in Database ( Sql Server DB 2000) with varchar field
Platform used : Microsoft Visual Studio 2010 (C#) Database : SQL Server 2008 R2
Getting error while inserting values into database (SQL Server 2008) Implicit conversion from data
When it comes to CRUD operations and your database (SQL Server '08), is it
how to add multimedia files(such as images) in a database(SQL SERVER or ORACLE)

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.