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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:25:19+00:00 2026-06-13T12:25:19+00:00

Here is a simplified version of my data: products: +—-+———–+ | id | name

  • 0

Here is a simplified version of my data:

products:
+----+-----------+
| id | name      |
+----+-----------+
|  1 | Product X |
|  2 | Product Y |
|  3 | Product Z |
+----+-----------+

categories:
+----+---------------+
| id | name          |
+----+---------------+
|  1 | Hotel         |
|  2 | Accommodation |
+----+---------------+

category_product
+----+------------+-------------+
| id | product_id | category_id |
+----+------------+-------------+
|  1 |          1 |           1 |
|  2 |          1 |           2 |
|  3 |          2 |           1 |
|  4 |          3 |           2 |
+----+------------+-------------+

How do I construct an efficient query that will only retrieve products that have both categories “Hotel” and “Accommodation” related (eg. Product X)?

I first tried a join approach

SELECT *
FROM products p
JOIN category_product cp
ON p.id = cp.product_id
WHERE cp.category_id = 1 OR cp.category_id = 2

^ This doesn’t work because it doesn’t contrain the query to containing both.

I have found an approach using sub-queries that works… but I’ve been warned against sub-queries for performance reasons:

SELECT *
FROM products p
WHERE
(
    SELECT id
    FROM category_product
    WHERE product_id = p.id
    AND category_id = 1
)
AND
(
    SELECT id
    FROM category_product
    WHERE product_id = p.id
    AND category_id = 2
)

Are there any better solutions (or how about alternatives)? I’ve considered de-normalizing categories to an extra column on products but would ideally like to avoid that. Hoping for a magic bullet solution!

UPDATE

I’ve run some of the (great) solutions provided in the answers:
My data is 235 000 category_product rows and 58 000 products and obviously benchmarks are always dependent on environment and indexes etc.

“Relational division” @podiluska

2 categories: 2826 rows  ~ 20ms 
5 categories: 46 rows ~ 25-30 ms 
8 categories: 1 rows ~ 25-30 ms 

“Where exists” @Tim Schmelter

2 categories: 2826 rows  ~ 5-7ms 
5 categories: 46 rows ~ 30 ms 
8 categories: 1 rows ~ 300 ms 

One can see the results start to diverge with having a greater number of categories thrown in. I’ll look at using “relational division” as it provides consistent results but implementation might cause me to look at “where exists” too (long format http://pastebin.com/6NRX0QbJ)

  • 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-13T12:25:20+00:00Added an answer on June 13, 2026 at 12:25 pm
    SELECT p.*
    FROM products p
         inner join 
    (
        select product_ID
        from category_product
        where category_id in (1,2)
        group by product_id
        having count(distinct category_id)=2
    ) pc
        on p.id = pc.product_id
    

    This technique is called “relational division”

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

Sidebar

Related Questions

Here is a simplified version of my data: > db.foo.insert({name : jim, locations :
Here's a simplified version of the 3 data sets I have: Set A =
Here is a simplified version of the problem: <div id=model-one-container data-bind=css: {foo: someModelOneProperty}> <div
Here is a simplified version of a consider the data structure below in Matlab:
Here is a simplified version of my Code: data Bookmark = Bookmark { url
Here is a simplified version of my data. This is the result of a
Here is simplified version of my requirement I have a java class say Processor
Here is a simplified version of what I'm trying to do through an expression.
Here is a simplified version of what I have (not working): prog.h: ... const
Here is a simplified version of the code I use. I need to rewrite

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.