Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 few questions about "joining"

Author  Topic 

smile
Starting Member

22 Posts

Posted - 2009-03-22 : 14:04:17
Hello!

Going through some queries I'm again confused with some topics around joining:
1) when we join tables the result, that we get, always contains unique records, even not having used "DISTINCT"?

2) when a joining query is executed (with whatever type of join we have), the first step is to build cross join between the mentioned rows and then applying the join condition, right? Then if we have more the two joined tables, first it's done a cross join between the first two, then cross join between the already obtained result and the next table and so on...and finally the join condition is applied?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-03-22 : 14:19:12
1. Records returned from a table join does not have to be unique. Run this example and you will see this.
declare @t1 table (id1 int );
declare @t2 table (id2 int );
insert into @t1 (id1) values (1);
insert into @t1 (id1) values (1);
insert into @t2 (id2) values (1);
select id1, id2 from @t1 t1 inner join @t2 t2 on t1.id1 = t2.id2

2. Logically, you can think of the query being executed as a cross join followed by applying the join conditions (and then including the rows from the preserved tables if you are using an outer join etc.) as you have described it. The physical query processing will be different in most cases. One of the best discussions of the logical query processing phases that I have seen is in Itzik Ben-Gan's book "Inside Microsoft SQL Server 2005 T-SQL Querying"
Go to Top of Page
   

- Advertisement -