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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:08:27+00:00 2026-06-18T12:08:27+00:00

This is a pretty basic question, but for some reason, I can’t reassure myself.

  • 0

This is a pretty basic question, but for some reason, I can’t reassure myself. I have a sql query that I want to ensure is acting as I expect.

question_records has a foreign key for both enrollments and questions

SELECT `question_records`.* FROM `question_records` 
  INNER JOIN `enrollments` ON `enrollments`.`id` = `question_records`.`enrollment_id` 
  INNER JOIN `questions` ON `questions`.`id` = `question_records`.`question_id` 
  WHERE (enrollments.id IN (10,20) 
         AND questions.id IN (500,600) 
         AND question_records.id not in (3000,4000))

What I want to make sure is that I will not get a question record with, for instance, an enrollment_id of 11 and a question_id of 500. In other words, does this inner join treat this where clause on a record-by record basis? More importantly, does it always treat it so? (I can come up with a test example, but want to make sure I’m not missing a case.

Edit: Additional narrative for clarity:

It is possible that there is a question_record in the database with an enrollment_id of 11 and a question_id of 500. If this query were not searching conditions on a record-by-record basis, it would match on the question_id of 500 and return the record. This is not the behavior that I want. I only want records for which both the enrollment_id and the question_id are matched for a given record. The answers I’ve gotten cover this condition.

  • 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-18T12:08:28+00:00Added an answer on June 18, 2026 at 12:08 pm

    A mental model for evaluating this query is that:

    1. You grab all rows from question_records, enrollments, and questions.
    2. You create a cartesian product of these three row sets. (Match everything with everything at first.)
    3. The INNER JOIN filters the rows of the cartesian product this based on whether the ON conditions are true.
    4. Then you filter this based on the final WHERE condition.
    5. Select the desired columns to form the final result.

    (Obviously the database will optimise that to something else entirely.)

    So, if a row matches the INNER JOIN..ON conditions, but does not match the WHERE one, it won’t appear in the final result. It has to match all the conditions.

    This might be easier to understand when you know that your INNER JOIN is equivalent to this:

    SELECT qr.* FROM question_records qr
                     enrollments      e
                     questions        q
      WHERE e.id = qr.enrollment_id
        AND q.id = qr.question_id
        -- your original WHERE condition
        AND enrollments.id IN (10,20) 
        AND questions.id IN (500,600) 
        AND question_records.id not in (3000,4000))
    

    (This form matches the mental model I outlined better.)

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

Sidebar

Related Questions

I'm pretty sure this is a basic question but for some reason i can't
I'm pretty sure this is a basic question but for some reason i can't
this is a pretty basic question but I can't seem to get it right.
This is a pretty basic question but I can't find a good answer for
I think this is a pretty basic question, but after Googling around I can't
This is probably a pretty basic question, but just something that I wanted to
I realize this question is pretty basic, but I'm really stuck. I have a
This is probably a pretty basic question, but just something that I wanted to
This is a pretty basic C++ design question: I have a class that contains
This is a pretty basic question but I'm still unsure: If I have a

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.