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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:37:08+00:00 2026-05-20T16:37:08+00:00

Let’s say I have 3 tables: Table 1 called states: id | state 1

  • 0

Let’s say I have 3 tables:

Table 1 called “states”:

id | state
1    italy
2    netherlands
3    russia

Table 2 called “hotels”:

id | hotel name | belongsToCountry
1    Green Hotel  2
2    Luxurious    2
3    Get Warm!    3

Table 3 called “free rooms”:

id | roomID | belongsToHotel
1    815      2
2    912      2
3    145      1
4    512      1
5    1200     3

Now, what I need to echo is this:

Netherlands has 4 free rooms.
Russia has 1 free room.

In words:
I need to make a list of all states which have at least 1 free room and I need to return the exact value of how many free rooms there are.

If anyone can help me with this, I’d be so grateful!

  • 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-20T16:37:09+00:00Added an answer on May 20, 2026 at 4:37 pm

    Let’s build the query step by step.

    First, let’s assemble the list of hotels and their free room count.

    SELECT hotels.id, COUNT(*)
      FROM hotels
           INNER JOIN free_rooms ON(hotels.id = free_rooms.belongsToHotel)
     GROUP BY hotels.id
    

    INNER JOINs force rows from the table on the “left” side of the join (hotels) only to be included in the result set when there is a corresponding table on the “right” (free_rooms). I’m assuming here that there will only be a row in free_rooms when the room is free.

    Having this, we can now join against the list-o-nations.

    SELECT hotels.id, COUNT(*), states.state
      FROM hotels
           INNER JOIN free_rooms ON(hotels.id = free_rooms.belongsToHotel)
           INNER JOIN states ON(hotels.belongsToCountry = states.id)
     GROUP BY hotels.id
    

    It should be noted, by the way, that you’ve made poor choices in naming these columns. states should be composed of id and state_name, hotels should be id, hotel_name, state_id, and free_rooms should be id, room_name and hotel_id. (I could also argue that states.id should be states.state_id, hotels.id should be hotels.hotel_id and free_rooms.id should be free_rooms.room_id because that makes the joins much easier…)

    If you need to represent a “belongs to” relationship, you’re actually looking for foreign key restraints. You should use those instead of special naming.

    *ahem* Where was I? Oh yes. The second query will result in a result set with three columns – the hotel id, the number of rooms in it, and the country it’s in. But, you just need the number of rooms per country, so let’s do one last change.

    SELECT COUNT(*), states.state
      FROM hotels
           INNER JOIN free_rooms ON(hotels.id = free_rooms.belongsToHotel)
           INNER JOIN states ON(hotels.belongsToCountry = states.id)
     GROUP BY states.state
    

    Only two changes. First, we’re now grouping together by state. Second, we’re no longer including the hotel id in the result set. This should get you the data you need, again assuming that there will never be a row in free_rooms when the room is not free.

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

Sidebar

Related Questions

Let say I have the following desire, to simplify the IConvertible's to allow me

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.