22

Till today my thoughts about the inner join were it will return the minimum number of rows that exist in tables satisfying a joining condition.

Ex. if table A contains 4 rows and table B contains 7 rows . i was expecting that 4 rows can be the maximum output if they satisfy the joining condition.

I just wrote an sp in which i was creating two temporary tables and was populating them. then i took an inner join of them but returning more rows (In my case 29 rows were returned i was expecting 4) After some search i found this link

which confirms that i can happen but i still wonder what are my options to limit the returned result.

Below is my stored procedure.

ALTER PROCEDURE [dbo].[GetDDFDetailOnSiteCol]
@siteId int,
@colNum int
AS
BEGIN
SET NOCOUNT ON;

create Table #portDetail
(
ddfId int,
portDetail nvarchar(50),
siteId int
)
Insert into #portDetail SELECT  ddf.id,  ddf.portDetail, site.Site_ID  from site
        inner join ddf ON site.Site_ID = ddf.siteCodeID 
        where ddf.siteCodeID = @siteId and ddf.colNo= @colNum
        order by colNo,blockNum,portRowNum,portColNum

create Table #portAllocationDetail
(
assigned_slot nvarchar(50),
siteId int
)
Insert into #portAllocationDetail 
SELECT  dbo.portList.assigned_slot, dbo.site.Site_ID
FROM dbo.portList INNER JOIN
 dbo.site ON dbo.portList.siteCodeID = dbo.site.Site_ID
 where dbo.site.Site_ID = @siteId

--select * from #portAllocationDetail   
Select #portDetail.ddfId,#portDetail.portDetail,#portAllocationDetail.siteId,#portAllocationDetail.assigned_slot FROM #portDetail 
INNER JOIN #portAllocationDetail 
ON
#portDetail.siteId = #portAllocationDetail.siteId
END
2
  • 1
    How do you determine which row(s) from Table B are returned? Sep 12, 2012 at 13:34
  • i write select statements. one still exist commented
    – Shah
    Sep 12, 2012 at 13:35

4 Answers 4

31

An inner join repeats each matching row in TableB for each row in TableA. So if there are 4 rows in TableA, and 7 in TableB, the maximum rowcount is 28.

Example at SQL Fiddle.

3
  • Then either TableA contains more than 4 rows or TableB contains more than 7 rows.
    – Andomar
    Sep 12, 2012 at 13:40
  • 1
    Hmm ok, How can i restrict it to four
    – Shah
    Sep 12, 2012 at 13:44
  • @shah, we can;t answer your question becasue we have to know what data in the second table you want to kepp and what you don't want to keep. You don't want to randomly select a value from that table so we have to know what business rule you want us to show you how to apply.
    – HLGEM
    Sep 12, 2012 at 13:49
1

An inner join returns the subset of the cartesian product of the tables that satisfy the condition. So for two tables A and B with rows n and m and a condition True, the join returns nxm rows and that is the maximum.

It is incredible how simple this explanation can be if only we used the correct terminology, but it takes me much more time to understand it "the simple way" which is how it is explained on most tutorials :(.

-2

Inner join results in duplicates too.

Use intersect operator if you want unique common values from both tables.

So, here intersect can even display null value if it is null on both sides.

2
  • if possible use the intersect operator in an example with the context of the question Apr 25, 2019 at 17:32
  • select * from portDetail pd,portAllocationDetail pad where pd.site_id = pad.site_id and pd.site_id in (select siteId from portDetail intersect select site_id from portAllocationDetail); May 21, 2019 at 21:34
-3

----------inner join means- The INNER JOIN keyword returns rows when there is at least one match in both tables.-----------------

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.