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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:07:18+00:00 2026-06-15T04:07:18+00:00

I am beginner at SQL and I am trying to create a query. I

  • 0

I am beginner at SQL and I am trying to create a query.

I have these tables:

CREATE TABLE Hospital (
    hid INT PRIMARY KEY,
    name VARCHAR(127) UNIQUE,
    country VARCHAR(127),
    area INT
);
CREATE TABLE Doctor (
    ic INT PRIMARY KEY,
    name VARCHAR(127),
    date_of_birth INT,
);
CREATE TABLE Work (
    hid INT,
    ic INT,
    since INT,
    FOREIGN KEY (hid) REFERENCES Hospital (hid),
    FOREIGN KEY (ic) REFERENCES Doctor (ic),
    PRIMARY KEY (hid,ic)
 );

The query is: What is the average in each country of the number of doctors working in hospitals of that country (1st column: each country, 2nd column: average)? 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-06-15T04:07:19+00:00Added an answer on June 15, 2026 at 4:07 am

    You first need to write a query that counts the doctors per hospital

    select w.hid, count(w.ic)
    from work w
    group by w.hid;
    

    Based on that query, you can retrieve the average number of doctors per country:

    with doctor_count as (
      select w.hid, count(w.ic) as cnt
      from work w
      group by w.hid
    )
    select h.country, avg(dc.cnt)
    from hospital h
      join doctor_count dc on h.hid = dc.hid
    group by h.country;
    

    If you have an old DBMS that does not support common table expressions the above can be rewritten as:

    select h.country, avg(dc.cnt)
    from hospital h
      join (
        select w.hid, count(w.ic) as cnt
        from work 
        group by w.hid
      ) dc on h.hid = dc.hid;
    

    Here is an SQLFiddle demo: http://sqlfiddle.com/#!12/9ff79/1

    Btw: storing date_of_birth as an integer is a bad choice. You should use a real DATE column.

    And work is a reserved word in SQL. You shouldn’t use that for a table name.

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

Sidebar

Related Questions

I have the following sql tables: Authors: Id Name 2 John Smith Books: Id
SQL beginner here. I have a query which takes around 10 seconds to run,
I am a beginner of SQL, having this table instructor: ID name dept_name salary
I am a beginner to SQL, so bear with me. I'm trying to create
am a beginner in C#. am trying to create a website i have a
SQL Server Beginner question: I'm trying to introduce a computed column in SQL Server
I'm a beginner at SQL and have this fairly easy conditional problem: Every installation
Please help this beginner here... I have a SQL Server 2008 R2 running on
I am a beginner in sql server.I have read about buffer cache in sql
I'm trying to update a field in one of our relational tables. The table

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.