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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:15:39+00:00 2026-06-10T20:15:39+00:00

I have two tables with data as – TestTable1 TestTable2 ———- ———- Id Name

  • 0

I have two tables with data as –

TestTable1                          TestTable2  
----------                          ----------  
Id  Name        DealDate            Id  Name        DealDate  
1   aaTable     2010-09-22          1   aaTable2    2010-09-23  
2   bbTable     2010-09-23          2   bbTable2    2010-09-24  
3   ccTable     2010-09-28          3   ccTable2    2010-09-26  
4   ddTable     2010-09-25          4   ddTable2    2010-09-27  

I want to have only two latest records from resultset of TestTable1 + TestTable2 to return this –

Id  Name        DealDate
4   ccTable     2010-09-28 
3   ddTable2    2010-09-27 

This is my current query

SELECT TOP 2 * FROM 
(
    SELECT * FROM
    (SELECT TOP 2 Id, Name, DealDate FROM TestTable1 ORDER BY DealDate DESC) T1
    UNION ALL 
    SELECT * FROM
    (SELECT TOP 2 Id, Name, DealDate FROM TestTable2 ORDER BY DealDate DESC) T2
) T1T2
ORDER BY DealDate DESC

Can anyone please guide me for a better way (performance-wise/readability wise) to achieve this?

P.S. Above tables are just to present the use-case, my actual tables have thousands of records.

Thank you!

  • 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-10T20:15:40+00:00Added an answer on June 10, 2026 at 8:15 pm

    The only thing I would change performance wise is to remove the ORDER BY and the TOP 2 in the inner selects – they are pointless.
    Other than that, your solution is the way to go. There shouldn’t be any performance problems as you are using UNION ALL and not UNION…

    SELECT TOP 2 * FROM 
    (
        SELECT Id, Name, DealDate FROM TestTable1
        UNION ALL 
        SELECT Id, Name, DealDate FROM TestTable2
    ) T1T2
    ORDER BY DealDate DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two data tables as follows dtOne ------------------------- ID | Name -------------------------- 101
I have two tables, first table structure (data) : -id -name -title -mail -source_id
I have two tables with one identical column name, but different data. I want
I have two tables of data, Project (ID, NAME, price), Expenses (ID, NAME, price,
I have two tables: data and comments data: ------------------- id | name | email
I have two tables containing peoples data, first name, last name etc. The first
i have two data tables with the same structure the first one has one
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
1I have the following two tables (sample data) and need to be able to
I have two tables set up in phpmyadmin- table userid and table data. The

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.