|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Usage of Join QuestionI am using a query by joiing 2 tables. I know that you can join 2
tables the traditional way also ie Select * from table1, table 2 where table1.Col1 = table2.Col2 is this query any way inferior to Select * from table1JOIN table 2 ON table1.Col1 = table2.Col2 THanks Thats just cosmetic, I would ratehr use the new ANSI syntax rather than
the old one because it is more readable, but if you wirte these two queries in QA, mark them and let the QA show you the execution plan you will see that the execution time for each query is 50 %. The Query optimizer builds the execution plan in the same way. HTH, jens Suessmeyer. Not sure on the "superiority" of either query, but they give different
results. The first query is a cross join, or cartesian product, of table1 and table2. If table1 has x columns and table2 has y columns, Select * from table1, table 2 where table1.Col1 = table2.Col2 produces a result set with (x + y) columns. Select * from table1 JOIN table 2 ON table1.Col1 = table2.Col2 produces a result set wtih (x + y) - 1 columns. The column that the JOIN is done on does not repeat. Show quote "SQL novice" wrote: > I am using a query by joiing 2 tables. I know that you can join 2 > tables the traditional way also > > ie > Select * from table1, table 2 where table1.Col1 = table2.Col2 > > is this query any way inferior to > > Select * from table1JOIN table 2 ON table1.Col1 = table2.Col2 > > THanks > > >Not sure on the "superiority" of either query, but they give different I did not under stand that. I am providing the DDL for my question.>results. The first query is a cross join, or cartesian product, of table1 and >table2. /* Create table table1 (Col1 int, Name nvarchar(255)) Create table table2 (Col2 int, Age int) Insert into table1 values (1,'John') Insert into table1 values (2,'Bill') Insert into table1 values (3,'Tom') Insert into table1 values (4,'Ben') Insert into table1 values (5,'Sid') Insert into table2 values (1,29) Insert into table2 values (2,24) Insert into table2 values (3,21) Insert into table2 values (4,22) Insert into table2 values (6,20) */ Select * from table1, table2 where table1.Col1 = table2.Col2 --is this query any way inferior to Select * from table1 JOIN table2 ON table1.Col1 = table2.Col2 I got the same result for both the queries. Like I already said, thats just the *OLD* syntax to write queries in
without using the JOIN Operator, that makes *NO* differences comparing the results *OR* gernerally the performance. Generally said you won´t create a cathesian product in here you are just creating a join based on the where query. HTH, Jens Suessmeyer. |
|||||||||||||||||||||||