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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:20:23+00:00 2026-05-12T06:20:23+00:00

As an example, I want to get the list of all items with certain

  • 0

As an example, I want to get the list of all items with certain tags applied to them. I could do either of the following:

SELECT Item.ID, Item.Name
FROM Item
WHERE Item.ID IN (
    SELECT ItemTag.ItemID
    FROM ItemTag
    WHERE ItemTag.TagID = 57 OR ItemTag.TagID = 55)

Or

SELECT Item.ID, Item.Name
FROM Item
LEFT JOIN ItemTag ON ItemTag.ItemID = Item.ID
WHERE ItemTag.TagID = 57 OR ItemTag.TagID = 55
GROUP BY Item.ID, Item.Name

Or something entirely different.

In general (assuming there is a general rule), what’s a more efficient approach?

  • 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-12T06:20:23+00:00Added an answer on May 12, 2026 at 6:20 am
    SELECT Item.ID, Item.Name
    FROM Item
    WHERE Item.ID IN (
        SELECT ItemTag.ItemID
        FROM ItemTag
        WHERE ItemTag.TagID = 57 OR ItemTag.TagID = 55)
    

    or

    SELECT Item.ID, Item.Name
    FROM Item
    LEFT JOIN ItemTag ON ItemTag.ItemID = Item.ID
    WHERE ItemTag.TagID = 57 OR ItemTag.TagID = 55
    GROUP BY Item.ID
    

    Your second query won’t compile, since it references Item.Name without either grouping or aggregating on it.

    If we remove GROUP BY from the query:

    SELECT  Item.ID, Item.Name
    FROM    Item
    JOIN    ItemTag
    ON      ItemTag.ItemID = Item.ID
    WHERE   ItemTag.TagID = 57 OR ItemTag.TagID = 55
    

    these are still different queries, unless ItemTag.ItemId is a UNIQUE key and marked as such.

    SQL Server is able to detect an IN condition on a UNIQUE column, and will just transform the IN condition into a JOIN.

    If ItemTag.ItemID is not UNIQUE, the first query will use a kind of a SEMI JOIN algorithm, which are quite efficient in SQL Server.

    You can trasform the second query into a JOIN:

    SELECT  Item.ID, Item.Name
    FROM    Item
    JOIN    (
            SELECT DISTINCT ItemID
            FROMT  ItemTag
            WHERE  ItemTag.TagID = 57 OR ItemTag.TagID = 55
            ) tags
    ON      tags.ItemID = Item.ID
    

    but this one is a trifle less efficient than IN or EXISTS.

    See this article in my blog for a more detailed performance comparison:

    • IN vs. JOIN vs. EXISTS
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I cascade delete one item, I want get list of all deleted items
Dear All, My list constains date/time items and I want to get the biggest
I want to get all photo fids albums. For example, I have albums and
I want to get all of the records of list view content. I have
I want to get 100 and example from this string ?connect:100/username:example/ I searched in
I want to get the single digits from two digits.for example if 36 I
I want to get N random numbers whose sum is a value. For example,
If I want to get a user that has the email address of 'me@example.com',
I got the chat example working from Atmosphere, and now I want to get
How can i mark menuitem in Application Bar. I want to get, for example,

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.