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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:04:43+00:00 2026-06-14T20:04:43+00:00

I have 2 simple tables that I would like to perform an INNER JOIN

  • 0

I have 2 simple tables that I would like to perform an INNER JOIN with, but the problem is that I’m getting duplicated (for the columns str1 and str2) results:

CREATE TABLE #A (Id INT, str1 nvarchar(50), str2 nvarchar(50))
insert into #A  values (1, 'a', 'b')
insert into #A  values (2, 'a', 'b')

CREATE TABLE #B (Id INT, str1 nvarchar(50), str2 nvarchar(50))
insert into #B values (7, 'a', 'b')
insert into #B  values (8, 'a', 'b')

select * from #A a
INNER JOIN #B b ON a.str1 = b.str1 AND a.str2 = b.str2  

It gave me 4 records when I really wanted 2.

What I got:
id | str1 | str2| id | str1 | str2
1 | a | b | 7 | a | b
2 | a | b | 7 | a | b
1 | a | b | 8 | a | b
2 | a | b | 8 | a | b

What I really wanted:
1 a | b | 7 | a | b
2 a | b | 8 | a | b

Can anyone help? I know this is achievable using a cursor and loop, but I’d like to avoid it and only use some type of JOIN if possible.

  • 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-14T20:04:44+00:00Added an answer on June 14, 2026 at 8:04 pm
    SELECT 
        a.id AS a_id, a.str1 AS a_str1, a.str2 AS a_str2, 
        b.id AS b_id, b.str1 AS b_str1, b.str2 AS b_str2
    FROM 
        ( SELECT *
               , ROW_NUMBER() OVER (PARTITION BY str1, str2 ORDER BY id) AS rn
          FROM #A 
        ) a
      INNER JOIN 
        ( SELECT *
               , ROW_NUMBER() OVER (PARTITION BY str1, str2 ORDER BY id) AS rn
          FROM #B 
        ) b 
        ON  a.str1 = b.str1 
        AND a.str2 = b.str2 
        AND a.rn = b.rn ;
    

    If you have more rows in one or the other tables for the same (str1, str2) combination, you can choose which ones will be returned by changing INNER join to either LEFT, RIGHT or FULL join.

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

Sidebar

Related Questions

I have two MySQL tables (product and price history) that I would like to
I have a very simple database table that joins two other tables in a
I'm working on a tool that will perform some simple transformations on programs (like
I have a simple table that has this structure: <table> <thead> <tr> <td>Some info</td>
Say I have a simple table that contains username, firstname, lastname. How do I
I have a simple table (lets call it Table1) that has a NVARCHAR field
Using MySQL, I have a simple table that logs the IP Address that users
I use SQL Server 2005. I have a simple logging table that my web
If I have a simple table where the data is such that the rows
I have a simple cakephp app with table articles that has a cat_id column

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.