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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:08:21+00:00 2026-06-14T04:08:21+00:00

I’m stumped on a SQL query and need your help. In plain English, I

  • 0

I’m stumped on a SQL query and need your help. In plain English, I need to return all of the hotels which offer packages that contain ALL the items I select.

Tables involved are:

  • hotels (hotel_id, hotel_name)
  • packages (package_id, hotel_id, package_name)
  • items (item_id, package_id, item_name)
  • package_items (package_id, item_id)

SAMPLE DATA

To help explain my problem, let’s look at this sample data to illustrate it more clearly.

Hotels table

----------------------
hotel_id | hotel_name
----------------------
1        | Hilton
2        | Westin
----------------------

Packages table

-----------------------------------------
package_id | hotel_id | package_name
-----------------------------------------
1          | 1        | Gold Package
2          | 1        | Silver Package
3          | 2        | Star Package
4          | 2        | Ultimate Package
-----------------------------------------

Items table

-------------------------------
item_id | item_name
-------------------------------
1       | Free Room Upgrade
2       | Dinner Included
3       | Spa Access
4       | Complimentary Papers
-------------------------------

Package Items table

---------------------
package_id | item_id
---------------------
1          | 2
1          | 3
1          | 4
2          | 2
2          | 3
3          | 4
4          | 1
4          | 2
4          | 3
4          | 4
---------------------

So, using this data you can see that for example, the Hilton’s Gold Package contains Dinner Included, Spa Access, and Complimentary Papers.

I’m building a feature on our frontend that lets the user filter for these various package items. A check box against each item, which when clicked should filter the results to show only the hotels which offer packages that contains ALL of the items selected by the user.

I’m a little stumped on the SQL for this. So far my research has brought me to the following sample code (found here: PostgreSQL where all in array):

SELECT conversation_id FROM conversations_users

 WHERE user_id IN (1,2)

 GROUP BY conversation_id HAVING COUNT(*) = 2

In this sample query the key part of the query is the HAVING COUNT() = 2 section. With this, I expect to force the query to ensure that ALL selected items must be matched, not just one, which is the default behavior of the query without the HAVING COUNT() = 2 section.

Using this approach, and the above table structures I have adapted this code to suit our own requirement, including the various JOIN queries etc. In the below example I’ve selected Dinner Included, Spa Access, and Complimentary Papers. I expect to get back both the Hilton and the Westin as both of these hotels offer packages which include ALL of these items. In the case of the Hilton their Gold Package offers what I’ve selected, and in the case of the Westin their Ultimate Package offers what I’ve selected.

Remember, I’m trying to find hotels which offer packages that contain ALL the items I’m searching for.

Here’s what I’ve got so far:

SELECT h.hotel_id, h.hotel_name FROM hotels h

  JOIN packages p       ON h.hotel_id   = p.hotel_id
  JOIN package_items pi ON p.package_id = pi.package_id
  JOIN items i          ON pi.item_id   = i.item_id

 WHERE i.item_id IN (2,3,4)

 GROUP BY h.hotel_id HAVING COUNT (*) = 3;

Unfortunately I’m not getting accurate results here and I’m stumped as to how to fix it. Serious kudos to anyone who can solve this problem for me.

  • 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-14T04:08:22+00:00Added an answer on June 14, 2026 at 4:08 am

    Update your last query as:

         SELECT h.hotel_id, h.hotel_name FROM hotels h
         JOIN packages p       ON h.hotel_id   = p.hotel_id
         JOIN package_items pi ON p.package_id = pi.package_id
         JOIN items i          ON pi.item_id   = i.item_id
         WHERE i.item_id IN (2,3,4)
         GROUP BY h.hotel_id 
         HAVING COUNT (DISTINCT i.item_id) = 3;
    

    This will return you the hotels having 3 different items in the offer.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

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.