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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:18:51+00:00 2026-06-13T10:18:51+00:00

I commonly have to query an extremely large table for multiple variables with many

  • 0

I commonly have to query an extremely large table for multiple variables with many conditions per variable. Often times, a variable will need to be queried for multiple ranges. For example, I could need all records of VAR_1 where VAR_1 is between 200-300, 350-400, 450-500.

Normally I would write this as follows, but have been told that using IN() instead of the multiple ORs would be much more efficient.

SELECT * FROM table
WHERE VAR_1 BETWEEN '200' AND '300' OR
      VAR_1 BETWEEN '350' AND '400' OR
      VAR_1 BETWEEN '450' AND '500'

Is there any way to condense this information and get rid of the ORs by nesting LIKE or BETWEEN clauses within an IN()?
Something along the lines of:

WHERE VAR_1 IN (BETWEEN '200' AND '300', BETWEEN '350' AND '400', BETWEEN '450' AND '500')

or

WHERE VAR_1 IN ('[200-300]','[350-400]','[450-500]')

I have tried things like these, but the syntax is clearly incorrect. Any ideas or directions you can point me in would be great, still very new to SQL.

  • 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-13T10:18:52+00:00Added an answer on June 13, 2026 at 10:18 am

    Some comments deny that an IN clause can be more efficient than using OR – which i believe is incorrect for the general case.

    It simply depends on the query optimizer of the database.

    If the query optimizer is clever enough, it will transforms the OR and IN clause to the same execution plan (if they are semantically equal).
    In this case, the further plan processing will have the same costs for both plans, one small difference might be that the costs of the transformation might incur a small difference.

    On the other hand, a query optimizer might not notice the strong correlation of the OR predicates and evaluate each range predicates independently (each one being a separate operator) in an execution plan, whereas an IN clause might be handled by a lookup table of all values in the in clause (only one operator) resulting in a noticeable runtime difference.

    So the overall answer is it heavily depends on your DBMS, i would suggest that you try out both versions in a small benchmark setting on your system.

    For SQL-92 there is no special syntax for something like

    WHERE VAR_1 IN ('[200-300]','[350-400]','[450-500]')
    

    so indeed you have only OR’s or IN lists.

    Please notice that the more general notation for

    WHERE VAR_1 IN ( '200', '201' )
    

    is

    WHERE VAR_1 = ANY ('200','201' )
    

    ( the operator “=” can be replaced by any other comparison operator, e.g. “<=” )

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

Sidebar

Related Questions

At work we have a very large code-base that we commonly export for a
I have seen two commonly used techniques for adding the directory of the file
I have a bunch of different objects that are commonly edited in the same
I commonly see people setting $_SESSION variables as $_SESSION['example']=$_REQUEST['something']; $example=$_SESSION['example']; is this redundant? I
The following is a scenario I commonly face: You have a set of commits
I have created a UserSiteBaseController that gets commonly used data and sets the data
I have a bunch of functions which returns commonly used UIViews in my application,
from How does a stackless language work? Haskell (as commonly implemented) does not have
I am trying to chain multiple compiled linq queries together. I have succeeded in
So far, my https deployments have commonly involved a naive lockdown of the entire

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.