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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:37:20+00:00 2026-05-14T02:37:20+00:00

Take the following query: select * from ( select a, b from c UNION

  • 0

Take the following query:

select * from
(
  select a, b
  from c
    UNION
  select a, b
  from d
)
where a = 'mung'
order by b

Will the optimizer generally work out that I am filtering a on the value ‘mung’ and consequently filter mung on each of the queries in the subquery.

OR

will it run each query within the subquery union and return the results to the outer query for filtering (as the query would perhaps suggest)

In which case the following query would perform better :

select * from
(
  select a, b
  from c
  where a = 'mung'
    UNION
  select a, b
  from d
  where a = 'mung'
)
order by b

Obviously query 1 is best for maintenance, but is it sacrificing much performace for this?

Which is best?

Edit
Sorry I neglected to add the order by clause in the queries to indicate why it all needed to be a subquery in the first place!

Edit
Ok, I thought it was going to be a simple answer, but I forgot I was talking about databases here! Running it through the analyser indicates that there is no optimization going on, but this could of course be because I only have 4 rows in my tables. I was hoping for a simpler yes or no.. Will have to set up some more complex tests.

I am on mySQL at the moment, but I am after a general answer here…

  • 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-14T02:37:20+00:00Added an answer on May 14, 2026 at 2:37 am

    Use:

    SELECT x.a, 
           x.b
      FROM TABLE_X x
     WHERE x.a = 'mung'
    UNION
    SELECT y.a, 
           y.b
      FROM TABLE_Y y
     WHERE y.a = 'mung'
    
    • The subquery is unnecessary in the given context
    • The query above will use indexes if available on column “a”; Using the subquery method, there’s no index to utilize
    • Use UNION ALL if you don’t have to be concerned with duplicates – it will be faster than UNION
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code : $gemng = $db->query(SELECT * FROM students ORDER BY
Take a look at the following mySQL query: SELECT fname,lname FROM users WHERE users.id
Take the following linq query: var summary = results.Select(r => new { TotalPopulation =
I have the following MySQL query: SELECT value_1, ( SELECT value_4 FROM table_1 WHERE
I have the following query: SELECT dashboard_data.headline, dashboard_data.message, dashboard_messages.image_id FROM dashboard_data INNER JOIN dashboard_messages
I need to take the following query and pull the total order counts and
i have the following query SELECT DISTINCT dr.Revision FROM tblActionHeader ah INNER JOIN tblActionType
I have the following query in mysql 5.1.41: select distinct table_schema from information_schema.tables where
I have the following query: SELECT * FROM my_users as U RIGHT JOIN subscribers
I have the following mysql query: EXPLAIN SELECT id, name, title, description, time FROM

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.