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.
| Author |
Topic |
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2002-02-16 : 08:42:14
|
| Whats the difference between the JOINS and AND, OR clause?...I tried to find from Help in T-SQL and BOL...But I couldnt understand properly..can you people explain how the joins work in comparison with an AND, OR clause...Mostly I use AND OR clauses in my queries...After posting my questions here and most of answers given to me were with joins.So now I'am trying to figure out how the Joins work...This may be a basic stuff but I want to know about it to use it in a proper way...Thanks in advance.KarunakaranDon't wait for things to happen,Make them to happen... |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2002-02-16 : 10:22:04
|
| Hi Karuna,When you use Joins the rows are filtered with the from clause itself then the where clause is applied whereas with only where clause the rows are filtered afterwards.To explain on that , if there are total of 50,000 records and the join will yield about 10,000 records and there are some other where clauses too. with joins the source of the data to filter is only 10,000 records coz the records are filtered before it goes to where . so when the where clause is applied it is applied only on the 10,000 records where as without joins it had to be applied on the whole 50,000 records thereby decreasing performance.from BOLThe rows selected by a query are filtered first by the FROM clause join conditions, then the WHERE clause search conditions, and then the HAVING clause search conditions. Inner joins can be specified in either the FROM or WHERE clause without affecting the final result.HTH--------------------------------------------------------------"Happiness is not something you experience, it's something you remember."Edited by - Nazim on 02/16/2002 10:23:32 |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2002-02-16 : 11:07:16
|
| Thanks Nazim...But still I couldnt understand how the LEFT JOIN and INNER JOINS work.To put it simple...I have 5 tables with following structure..tbl1 recid,recnametbl2 recid,splidtbl3 recid,catidtbl4 recid,cityidtbl5 recid,countryidNow I need to pick the recid from tbl1 whereas the recid in tbl2,tbl3 should be present in tbl1,the recid in tbl2,tbl3 should also be present in either tbl4 or tbl5I have the query like this...select count(distinct a.recid) from tbl1 a LEFT JOIN tbl2 b ON a.recid = b.recid AND b.splid IN (37) LEFT JOIN tbl3 c ON a.recid = c.recid AND c.catid IN (66,67,68,69,70) INNER JOIN tbl4 d ON a.recid = d.recid AND d.cityid IN (85,145) INNER JOIN tbl5 e ON a.recid = e.recid AND e.countryid IN (33) WHERE a.middle=1 and COALESCE(b.recruiter_id, c.recruiter_id, d.recruiter_id) IS NOT NULLThis query is also an answer posted to one of my questions here.I made few changes,without understanding how it works...But I'am not sure whether it meets my requirement...Unless I understand how the Inner joins and left joins work..I dont think I can solve the problem myself.KarunakaranDon't wait for things to happen,Make them to happen... |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-02-16 : 11:52:50
|
| Think of the result set being a filtered cartesian product.Say table a has 3 recs and table b 4 recsa(i,j)1,11,22,1b(i,j)1,11,23,13,2the cartesian product is all possible combinations of rows from both tables i.e. 12 recsa(i,j)b(i,j)1,1,1,11,1,1,21,2,3,11,2,3,21,2,1,11,2,1,21,2,3,11,2,3,22,1,1,12,1,1,22,1,3,12,1,3,2the inner join only takes rows for which the join criteria is fulfilleda inner join b on a.i = b.igivesa(i,j)b(i,j)1,1,1,11,1,1,21,2,1,11,2,1,2the left outer join does the same thing but also returns at least one copy of every row from the left table - filling in the values from the other table with null if there is no matching row. In the above query there is no entry for 2,1 from table a soa left outer join b on a.i = b.igivesa(i,j)b(i,j)1,1,1,11,1,1,21,2,1,11,2,1,22,1,null,nullAnother way of thinking of it is a left outer join returns every row from the left table and all rows from the right table the fulfill the join criteria.Your query is of the forma(i,j)b(i,j)1,1,1,11,1,1,21,2,1,11,2,1,22,1,null,nulla left outer join b on a.i = b.i and b.i = 1which would just drop the rows which do not satisfy the criteria and add any missing rows from a.Note that this is different from a left outer join b on a.i = b.i where b.i = 1This would perform the left outer join first then filter on the where clause - it would omit all rows where b.i <> 1 - i.e. drop all the rows where b.i = null and so would be the same as an inner join.==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
karuna
Aged Yak Warrior
582 Posts |
Posted - 2002-02-16 : 12:09:22
|
| As of now I feel like I'am clear with this...If I have any doubts I'll come back...Thank you guys...KarunakaranDon't wait for things to happen,Make them to happen... |
 |
|
|
|
|
|
|
|