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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:34:25+00:00 2026-05-31T08:34:25+00:00

say we have the following SQL: select * from ( select Id from table1

  • 0

say we have the following SQL:

select * from (
    select Id from table1
    union all
    select id from table2
    union all
    select id from table3
) as X where Id in (1,2,3)

Is the SQL optimizer smart enough to apply “where id in (1,2,3)” to each table before doing the union? Can we make an assumption about other DB vendors such as Oracle on this? Assumptions are always dangerous, but, thought I’d ask anyway.

  • 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-31T08:34:26+00:00Added an answer on May 31, 2026 at 8:34 am

    Try running this query in SQL Server Management Studio with Include Actual Execution Plan enabled. See if the inner selects do an index seek on these keys.

    I setup a test case with 3 tables that had ID as their clustered primary key. The execution plan did show clustered index seeks on the three IDs selected.

    However, if you really want to be sure the inner queries optimize their plans this way, you can put a WHERE clause on each sub-query. For example:

    select * from
    (
        select Id from table1 where Id in (1,2,3)
        union all
        select id from table2 where Id in (1,2,3)
        union all
        select id from table3 where Id in (1,2,3)
    ) as X
    

    Also note that the above union all will potentially return duplicates if more than one of the tables have a matching Id. If this is a problem, you can change union all to just union like so.

    select * from
    (
        select Id from table1 where Id in (1,2,3)
        union
        select id from table2 where Id in (1,2,3)
        union
        select id from table3 where Id in (1,2,3)
    ) as X
    

    I cannot guarantee that behavior on other DB systems, but I’d be surprised if their query optimizers didn’t do the same thing.

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

Sidebar

Related Questions

I have the following SQL query: select ID, COLUMN1, COLUMN2 from (select ID, COLUMN1,
I have written the following SQL statement in MySQL : USE my_database; SELECT *
In Sql, let's say I have the following procedure GetCars ( @Ids nvarchar(MAX) =
Lets say we have the following: create view view_1 as ( select key, data
Ok, say I have a query: SELECT * FROM TABLE_AWESOME WHERE YEAR = :AMAZINGYEAR;
Lets say i have the following bit of code import java.sql.Connection; import java.sql.DriverManager; import
Let's say I have following query: SELECT Id, Name, ForeignKeyId, (SELECT TOP (1) FtName
Let's say we have a table (EnsembleMembers) in an SQL database with the following
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Say we have the following method: private MyObject foo = new MyObject(); // and

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.