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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:25:17+00:00 2026-05-25T00:25:17+00:00

Suppose I have a table1 like this: id | itemcode ————- 1 | c1

  • 0

Suppose I have a table1 like this:

id | itemcode
-------------
1  | c1
2  | c2
...

And a table2 like this:

item | name
-----------
c1   | acme
c2   | foo
...

Would the following two queries return the same result set under every condition?

SELECT id, itemcode 
FROM table1 
WHERE itemcode IN (SELECT DISTINCT item 
                  FROM table2 
                  WHERE name [some arbitrary test])

SELECT id, itemcode 
FROM table1 
   JOIN (SELECT DISTINCT item 
          FROM table2
          WHERE name [some arbitrary test]) items 
         ON table1.itemcode = items.item

Unless I’m really missing something stupid, I’d say yes. But I’ve done two queries which boil down to this form and I am getting different results. There are some nested queries using WHERE IN, but for the last step I’ve noticed a JOIN is much faster. The nested queries are all entirely isolated so I don’t believe they are the problem, so I just want to eliminate the possibility that I’ve got a misconception regarding the above.

Thanks for any insights.

EDIT

The two original queries:

SELECT imitm, imlitm, imglpt 
    FROM jdedata.F4101 
    WHERE imitm IN 
  (SELECT DISTINCT ivitm AS itemno 
       FROM jdedata.F4104 
       WHERE ivcitm IN 
    (SELECT DISTINCT ivcitm AS legacycode 
             FROM jdedata.F4104 
              WHERE ivitm IN 
      (SELECT DISTINCT tritm 
               FROM trigdata.F4101_TRIG)
    )
  )


SELECT orig.imitm, orig.imlitm, orig.imglpt 
    FROM jdedata.F4101 orig 
        JOIN 
        (SELECT DISTINCT ivitm AS itemno 
        FROM jdedata.F4104 
        WHERE ivcitm IN 
              (SELECT DISTINCT ivcitm AS legacycode 
              FROM jdedata.F4104
               WHERE ivitm IN 
                 (SELECT DISTINCT tritm 
                  FROM trigdata.F4101_TRIG))) itemns 
ON orig.imitm = itemns.itemno

EDIT 2

Although I still don’t understand why the queries returned different results, it would seem our logic was flawed from the beginning since we were using the wrong columns in some parts. Mind that I’m not saying I made a mistake interpreting the queries as written above or had some typo, we just needed to select on some different stuff.

Normally I don’t rest until I get to the bottom of things like these, but I’m very tired and am entering my first vacation since January that spans more than one day, so I can’t really be bothered searching further right now. I’m sure the tips given here will come in handy later. Upvotes have been distributed for all the help and I’ve accepted Ypercube’s answer, mostly because his comments have led me the furthest. But thanks all round! If I do find out more later, I’ll try to remember pinging back in.

  • 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-25T00:25:18+00:00Added an answer on May 25, 2026 at 12:25 am

    Since table2.item is not nullable, the 2 versions are equivalent. You can remove the distinct from the IN version, it’s not needed. You can check these 3 versions and their execution plans:

    SELECT id, itemcode FROM table1 WHERE itemcode IN
      ( SELECT item FROM table2 WHERE name [some arbitrary test] )
    
    SELECT id, itemcode FROM table1 JOIN
      ( SELECT DISTINCT item FROM table2 WHERE name [some arbitrary test] )
      items ON table1.itemcode = items.item
    
    SELECT id, itemcode FROM table1 WHERE EXISTS
      ( SELECT * FROM table2 WHERE table1.itemcode = table2.item 
                               AND (name [some arbitrary test]) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose I have a table like this: table1: name favorite_music a country b rock
Suppose I have a table, Foo, that looks like this: ID | Name |
Suppose I have a table like this ID Name 1 RJ 2 Hello 3
Suppose I have this table [Table1] Name Mark ------- ------ ABC 10 DEF 10
Suppose I have a database table with two fields, foo and bar. Neither of
Suppose I have a table like this.. ColA ColB ColC ---------------------- A 100 1
Suppose I have a 2 column table like this: | user_id | int(11) |
suppose I have the following source table (called S): name gender code Bob 0
Suppose I have a table view, and I want to implement something like this:
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`

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.