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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:14:40+00:00 2026-05-16T14:14:40+00:00

I have a table with some data in it: ColA | ColB | ColC

  • 0

I have a table with some data in it:

 ColA | ColB | ColC
------+------+------
  1   |  A   |  X
  2   |  A   |  Y
  3   |  B   |  Y
  4   |  C   |  Y
  5   |  C   |  Z
  6   |  D   |  Y
  7   |  D   |  Z

I want to query to get all of the rows where ColB and ColC as a pair match a condition:

SELECT * FROM [Table] 
WHERE (ColB = A AND ColC = Y)
OR (ColB = B AND ColC = Y)
OR (ColB = C AND ColC = Y)
OR (ColB = D AND ColC = Z)

this should return rows 2, 3, 4 and 7.


The pairs of values for (ColB, ColC) could potentially be large (in the region of ~100). Is there a more efficient way of querying for this data other than a large query with lots of OR conditions?

I am hoping there is a way of using an equivalent of a Tuple, meaning I can do something like:

SELECT * FROM [Table] 
WHERE (ColB, ColC) IN ({A, Y}, {B, Y}, {C, Y}, {D, Z})

Any ideas?


EDIT: (to answer some questions in the comments)

The fields for ColB and ColC store guids and are declared as uniqueidentifier types.
This needs to work on SQL Server 2005 upwards (all editions).
The table has in the order of millions of rows, and I’m not averse to adding any indices that are required for this to work.

  • 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-16T14:14:41+00:00Added an answer on May 16, 2026 at 2:14 pm

    You could do this. (If you are on SQL Server 2008 you could use the values row constructors rather than union alls)

    You’d need to check the query plan to see if it was any more efficient.

    SELECT * /*But don't use star*/
    FROM [Table] 
    JOIN 
    (
    SELECT 'A' AS BMatch, 'Y' AS CMatch UNION ALL
    SELECT 'B' AS BMatch, 'Y' AS CMatch UNION ALL
    SELECT 'C' AS BMatch, 'Y' AS CMatch UNION ALL
    SELECT 'D' AS BMatch, 'Z' AS CMatch UNION ALL ...
    ) Matches
    ON ColB = BMatch AND ColC = CMatch
    

    You say in the comments that the combination of ColB and ColC is unique so (assuming your table already has a clustered index) I would imagine if you create a unique nonclustered index on either (colb, colc) or (colc, colb) that the above should give you a plan with 100 index seeks followed by 100 bookmark lookups. If it doesn’t you could try adding an index hint to get it to use the new index. You’d need to compare the I/O for that with the I/O of a full scan as lots of ors would likely give you.

    The cost of the bookmark lookups could potentially be avoided by including the additional required columns in the non clustered index. You’ve used * though so I don’t know how viable this would be. You’d need to balance the benefit to this query against possible disbenefits to data modification operations.

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

Sidebar

Related Questions

I have the following table, TableA, with some data ColA ColB 0 5 0
I have a table named location , and I want to get some data
Given I have table with some data... I have a query which is used
Ok, I have a table which contains multiple rows. Each row contains some data
I have a table with some dynamically generated data. The issue is I want
I have a table that I need to get some specific data from for
I have one table with some data. After some event I want to highlight
I have a PostgreSQL table containing some binary data. I need to get the
I have a table with some persistent data in it. Now when I query
I have a table view and it is populated by some data. I want

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.