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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:46:47+00:00 2026-06-12T04:46:47+00:00

In this example I am joining two tables. DECLARE @AA TABLE ( A1 INT,

  • 0

In this example I am joining two tables.

DECLARE @AA TABLE
        (
            A1 INT,
            A2 INT
        )       
DECLARE @BB TABLE
        (
            B1 INT,
            B2 INT
        )

INSERT INTO @AA values (1,1)    
INSERT INTO @AA values (2,2)    
INSERT INTO @AA values (3,3)    

INSERT INTO @BB values (1,1)    
INSERT INTO @BB values (2,2)    
INSERT INTO @BB values (3,3)

SELECT A1, A2, B1, B2 from @AA a
        JOIN @BB b on b.B1 = a.A1
        where 1=1
        and a.A1 in (1,2)
        and b.B1 in (select max(bb.B1) from @BB bb JOIN @AA aa on bb.B1 = aa.A1 where aa.A1 in (1,2))

The above code is working since I am looking to get the result: “2,2,2,2”

Is there a more efficient way to do this? The inner select is pretty much the same as the outer select and to me it doesn’t look very nice.

  • 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-12T04:46:48+00:00Added an answer on June 12, 2026 at 4:46 am

    You can just use

    SELECT TOP 1 WITH TIES A1,
                           A2,
                           B1,
                           B2
    FROM   @AA a
           JOIN @BB b
             ON b.B1 = a.A1
    WHERE  a.A1 IN ( 1, 2 )
    ORDER  BY b.B1 DESC 
    

    Rather than running the query once to get the max(B1) then using the result of that in a filter against the same query.

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

Sidebar

Related Questions

This example is taken from w3schools . CREATE TABLE Persons ( P_Id int NOT
I need to join two tables called MSISDN and RANGES. Example: MSISDN table: MSISDN
In this example we have 3 related tables on a SQLite database: CREATE TABLE
How can I efficiently update the table based on values from a join table
I have two tables, User and Company . I have joining them like this:
I am trying to join two tables using mysql, this is not difficult, my
I have problem with joining tables, here are example tables: Table A: ( 30
I need some help joining two table. I've got: my_type_table , which has columns:
I'm trying to force Linq to preform an inner join between two tables. I'll
I have run into a problem joining two strings in Python. I have some

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.