Quantcast
Channel: Questions & Answers: SQL Server: Join two tables returning only one row from the second table
Browsing latest articles
Browse All 6 View Live

SQL Server: Join two tables returning only one row from the second table

TABLE1ID (PK) Name 1001 SamTABLE2IDadd (FK) Country 1001 USA 1001 UKMy query:SELECT A.Name, B.CountryFROM TABLE1 AINNER JOIN (SELECT TOP 1 * FROM TABLE2 WHERE IDadd = A.ID) AS BON B.IDadd =...

View Article



You have your parentheses wrong

SELECT Name, CountryFROM Table1 INNER JOIN Table2 ON IDAdd = IDI think that's what you mean and what will give you what you want. Get back to us if that's not really what you want.

View Article

Reponse To Answer

Thank you for the reply.Your query will return two (2) rows, since there are two records with IDadd = 1001 in TABLE2. What I want is to get only 1 row from TABLE2 and join it to TABLE1 so I could...

View Article

Which row do you want?

Top 1 does not make sense without order by, you will just get a random row, which might or might not be the first row you entered.Here I select the least (alfabetic) country per personSELECT A.Name,...

View Article

Reponse To Answer

Why do you only want one row from table 2? I don't understand the purpose of your query.

View Article


Reponse To Answer

Thanks for your reply! Your first query above got it working.SELECT A.Name, B.CountryFROM TABLE1 AINNER JOIN (SELECT IDadd,MIN(Name) Name FROM TABLE2 GROUP BY IDadd) AS BON B.IDadd = A.ID

View Article
Browsing latest articles
Browse All 6 View Live


Latest Images