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 |
anna a
Starting Member
3 Posts |
Posted - 2001-12-20 : 07:22:56
|
hello. help please, anyone. i have 3 tables: A, B, and C, each with 2 integer fields, that are related by primary and foreign key IDs:AA1_PK_IDA2BB1_FK_IDB2CC1_FK_IDC2i want to get all the 2nd fields that are > 100 from those records with matching key/IDs, something like:SELECT A2, B2, C2FROM A, B, CWHERE A.A1_PK_ID = B.B1_FK_IDAND A.A1_PK_ID = C.C1_FK_IDAND A.A2 > 100AND B.B2 > 100AND C.C2 > 100is that legal? or how about this?:SELECT A2, B2, C2FROM ((A INNER JOIN BON A.A1_PK_ID = B.B1_FK_ID)INNER JOIN CON A.A1_PK_ID = C.C1_FK_ID)WHERE A2 > 100AND B2 > 100AND C2 > 100or this?:SELECT A2, B2FROM A INNER JOIN BON A.A1_PK_ID = B.B1_FK_IDAND A.A2 > 100AND B.B2 > 100UNIONSELECT C2FROM A INNER JOIN CON A.A1_PK_ID = C.C1_FK_IDAND A.A2 > 100AND C.C2 > 100or this?:SELECT A2, B2, C2FROM(SELECT A2FROM AWHERE A.A2 > 100)INNER JOIN(SELECT B2FROM BWHERE B.B2 > 100)ON A.A1_PK_ID = B.B1_FK_IDINNER JOIN(SELECT C2FROM CWHERE C.C2 > 100)ON A.A1_PK_ID = C.C1_FK_IDwhat's the legal and most efficient way to this?thanx, anna a |
|
Nazim
A custom title
1408 Posts |
Posted - 2001-12-20 : 08:03:55
|
SELECT A2, B2, C2FROM(SELECT A2FROM AWHERE A.A2 > 100)INNER JOIN(SELECT B2FROM BWHERE B.B2 > 100)ON A.A1_PK_ID = B.B1_FK_IDINNER JOIN(SELECT C2FROM CWHERE C.C2 > 100)ON A.A1_PK_ID = C.C1_FK_IDyou have to give aliases for this to worklikeSELECT A2, B2, C2FROM(SELECT A2FROM AWHERE A.A2 > 100) aINNER JOIN(SELECT B2FROM BWHERE B.B2 > 100) bON A.A1_PK_ID = B.B1_FK_IDINNER JOIN(SELECT C2FROM CWHERE C.C2 > 100) cON A.A1_PK_ID = C.C1_FK_IDthis should work faster then the others. its just my thought i havent cross checked it. but you can use query analyzer and check its execution plan by using Show Execution Plan.HTH-------------------------"Success is when Preparedness meets Opportunity" |
|
|
anna a
Starting Member
3 Posts |
Posted - 2001-12-20 : 16:14:24
|
thanx, Nimaz. that makes sense: the derived table is always <= the original table, so if the derived tables < original tables, then joining them should be faster.btw, since SQL is case-insensitive, if the original tables are:A, B, Cshould i name the derived tables aliases something like:aa, bb, ccdoes the SQL engine care?also, should i use the aliases (or the originals) for the ON clause?:SELECT aa.A2, bb.B2, cc.C2FROM(SELECT A2FROM AWHERE A2 > 100) aaINNER JOIN(SELECT B2FROM BWHERE B2 > 100) bbON aa.A1_PK_ID = bb.B1_FK_IDINNER JOIN(SELECT C2FROM CWHERE C2 > 100) ccON aa.A1_PK_ID = cc.C1_FK_IDthanx, anna a |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2001-12-21 : 23:32:10
|
oops! you got my name wrong its Nazim not Nizam quote: thanx, Nimaz. that makes sense: the derived table is always <= the original table, so if the derived tables < original tables, then joining them should be faster.
Yeah, whenever you give a Alias name you got to stick to it, you cant use the original name of the table, but you can have a alias name same as the tablename(though that will look foolish to have it same, just for heck of clarifying)quote: btw, since SQL is case-insensitive, if the original tables are:A, B, Cshould i name the derived tables aliases something like:aa, bb, ccdoes the SQL engine care?
yeah whenever you use join clause it becomes necessary to use on clause otherwise Sql reports a errorquote: also, should i use the aliases (or the originals) for the ON clause?:SELECT aa.A2, bb.B2, cc.C2FROM(SELECT A2FROM AWHERE A2 > 100) aaINNER JOIN(SELECT B2FROM BWHERE B2 > 100) bbON aa.A1_PK_ID = bb.B1_FK_IDINNER JOIN(SELECT C2FROM CWHERE C2 > 100) ccON aa.A1_PK_ID = cc.C1_FK_IDthanx, anna a
-------------------------"Graz's Baby is my Master:)" |
|
|
anna a
Starting Member
3 Posts |
Posted - 2001-12-23 : 11:48:57
|
quote: oops! you got my name wrong its Nazim not Nizam :(
so sorry, Nazim -- it was late in the day. :) thanx for the clarification.anna a |
|
|
|
|
|
|
|