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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:19:47+00:00 2026-05-30T09:19:47+00:00

I have the tables question, topic and question_has_topic (many-to-many relationship). In my application admins

  • 0

I have the tables question, topic and question_has_topic (many-to-many relationship). In my application admins see a breakdown for questions grouped by their topics and they select how many from each they’d like the system to randomly select to create a test.
This is the kind of table they see:

+-----------------------+---------------------+------------+
|        Topics         | Questions available | Selection: | 
+-----------------------+---------------------+------------+
| health,safety,general |                  13 |            |
| health                |                   3 |            |
| safety                |                   7 |            |
| general               |                   1 |            |
+-----------------------+---------------------+------------+

The count is unique for the particular grouping of topics. Anyway, once they make the selection I need a SQL statement which will select questions that correspond to the given grouping of topics.
I.e. I might need 3 questions which have the topics health,safety and general.
I was doing some research online and I think that what I’m trying to do is known as divide in relational algebra and here is my attempt for an arbitrary grouping of topicids:

select questionid from question_has_topic
where not exists (
    select questionid from question_has_topic
    where topicid not in (8,9,10))

The result is empty, although there are 2 questions in the database that have all these topic ids which tells me this isn’t working. I was following the example from this link

  • 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-30T09:19:48+00:00Added an answer on May 30, 2026 at 9:19 am

    EDIT: removed my old post since I misread the question.


    This is a technique I’ve used in the past:

      SELECT qht.questionid
        FROM question_has_topic AS qht
       WHERE qht.topicid IN (8,9,10)
    GROUP BY qht.questionid
      HAVING COUNT(*) = 3 AND
             COUNT(*) = (SELECT COUNT(*) FROM question_has_topic AS dupe
                         WHERE dupe.questionid = qht.questionid)
    

    where 3 corresponds to the number of topics in the given group. This assumes that each (questionid, topicid) pair in question_has_topic is unique (which it should be in a many-to-many relationship table).

    The way this query works is by first selecting any question that has at least one of the desired topics assigned to it (WHERE qht.topicid IN (8,9,10)), then grouping by the questionid. The first HAVING clause (COUNT(*) = 3) can only be true if a given question has all three topics assigned to it (since we assume duplicates aren’t allowed in this table). The second HAVING clause checks the total number of topics that are assigned to the question. This is to guard against the case where, for example, a question may have topics 8, 9, 10, and 11 assigned to it.

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

Sidebar

Related Questions

My DB have two tables - Question and Topic. To implement many-to-many relation, there
I have 2 mysql tables : Question with the following columns : id, question,
I have two tables userdetails and blog question The schema is UserDetails: connection: doctrine
I have quick question for you SQL gurus. I have existing tables without primary
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
Basic question! I have 2 tables PRODUCE +-----+--------------+ | id | fruit_name | +--------------------+
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers
Hello guys sorry if the question looks stupid to you. i have 3 tables
In a follow-up to a previous question, let's say I have 3 tables, A,
I have a question: I have two MSSQL tables, items and states, that are

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.