Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

SQL & the JOIN Operator Expand / Collapse
Author
Message
Posted Wednesday, October 7, 2009 7:59 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:00 AM
Points: 35,547, Visits: 32,137
Chris.Strolia-Davis (10/7/2009)
Excellent article.

You mentioned not knowing a real world application of the CROSS JOIN.

In my experience, this is typically used for creating test data.

Sometimes you need to test data in all sorts of different configurations. By using a cross join, you can set up the different parameters and try all combinations.

Additionally, if you are trying to create bogus data for a test environment, this is one way of taking data from different parts of the real data and generating new data that is not actually real.

In many cases, this type of join is used on temporary or memory based tables in a batch since the data it produces often needs to go through additional transformation and filtering before it is useful.

I guess, technically, it isn't used in a "real world" application, but it is used for real world issues.

The real world applications for using CROSS JOINS are many and varied. Most of them revolve around the use of a Tally Table (Numbers Table) to do things like make a Tally CTE which in turn would be cross joined to a delimited column to do splits or used to generate contiguous dates, etc. When limited by Triangular self joins (about half a cross join but still uses CROSS JOIN), they can be used to generate "schedule pairs" and a whole lot more. And, you're also correct... they can be used to very quickly generate very large volumes of constrained randomized test data. It's not uncommon to see some of the frequent posters generate a million row test table to make their point about a performance problem/solution. Rog_os also pointed out a frequent use above.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #799181
Posted Wednesday, October 7, 2009 8:03 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 10:16 AM
Points: 808, Visits: 1,993
Great article. Very good primer on the subject. When you do your next one on advanced joins please talk about moving thing from the WHERE to the ON for better performance. Case in point I needed a list of all CUSTOMERs who had 'CREDIT' type ORDERs placed in the last 30 days. So instead of:
WHERE [ORDER].TypeId = 3 AND ...

use
INNER JOIN [ORDER] ON [ORDER].CustomerID = [CUSTOMER].Id AND [ORDER].TypeId = 3



ATB

Charles Kincaid

Post #799183
Posted Wednesday, October 7, 2009 8:04 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:00 AM
Points: 35,547, Visits: 32,137
Nice article, Wagner! Articles of this nature should be required reading for anyone just starting out in SQL and those that enjoy a refresher. Well done. In the vein of "One picture is worth a thousand words", you did a great job with the graphics. Thanks for taking the time.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #799184
Posted Wednesday, October 7, 2009 8:08 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 10, 2014 9:36 AM
Points: 1,414, Visits: 4,544
is there any performance difference doing a union all compared to a join when you need to get all the rows from two or more tables? i have a database where i do union all on some data in 20 tables or so when running a report and it seems to take a long time

https://plus.google.com/100125998302068852885/posts?hl=en
http://twitter.com/alent1234
x-box live gamertag: i am null
[url=http://live.xbox.com/en-US/MyXbox/Profile[/url]
Post #799186
Posted Wednesday, October 7, 2009 8:18 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 1:39 PM
Points: 245, Visits: 735
We use "cross joins", actually no join at all, when we are creating our dimensions in our data warehouse.


<><
Livin' down on the cube farm. Left, left, then a right.
Post #799195
Posted Wednesday, October 7, 2009 8:26 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, December 4, 2013 3:54 PM
Points: 67, Visits: 230
Yet another article that makes people think that table aliases (t1 and t2) are part of the required syntax. This first Join statement:

SELECT t1.key1, t1.field1 as Name, t1.key2 as T1Key,
t2.key2 as T2Key, t2.field1 as City
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.key2 = t2.key2

Should be written like this:

SELECT Table1.key1, Table1.field1 as Name, Table1.key2 as T1Key,
Table2.key2 as T2Key, Table2.field1 as City
FROM Table1
INNER JOIN Table2 ON Table1.key2 = Table2.key2

Isn't that easier to read?

In this simple example, why in the WORLD are you including table aliases for Table1 and Table2? They are COMPLETELY unneccessary. When new users are exposed to table aliases in this kind of setting, they naturally think the table aliases are required.

It gets more important when the tables are named ClientStatus and ClientOrders and people start aliasing them as "cs" and "co", or even worse, as "a" and "b".

PLEASE don't teach that table aliases are a required part of the syntax. When they are required, they are required, but not in this simple example, and here they add nothing -- and they DETRACT from human readability -- in such simple examples. The human mind has to keep track of which alias goes with which table, and it gets hard when there are 5 or 6 tables involved.

It's also good to see a <= join here. That's often left out of examples. Don't forget to also teach that you can join tables on two conditions (using AND/OR), not just one.

Thanks.

David Walker
Post #799202
Posted Wednesday, October 7, 2009 8:40 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 10, 2014 9:36 AM
Points: 1,414, Visits: 4,544
David Walker-278941 (10/7/2009)
Yet another article that makes people think that table aliases (t1 and t2) are part of the required syntax. This first Join statement:

SELECT t1.key1, t1.field1 as Name, t1.key2 as T1Key,
t2.key2 as T2Key, t2.field1 as City
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.key2 = t2.key2

Should be written like this:

SELECT Table1.key1, Table1.field1 as Name, Table1.key2 as T1Key,
Table2.key2 as T2Key, Table2.field1 as City
FROM Table1
INNER JOIN Table2 ON Table1.key2 = Table2.key2

Isn't that easier to read?

In this simple example, why in the WORLD are you including table aliases for Table1 and Table2? They are COMPLETELY unneccessary. When new users are exposed to table aliases in this kind of setting, they naturally think the table aliases are required.

It gets more important when the tables are named ClientStatus and ClientOrders and people start aliasing them as "cs" and "co", or even worse, as "a" and "b".

PLEASE don't teach that table aliases are a required part of the syntax. When they are required, they are required, but not in this simple example, and here they add nothing -- and they DETRACT from human readability -- in such simple examples. The human mind has to keep track of which alias goes with which table, and it gets hard when there are 5 or 6 tables involved.

It's also good to see a <= join here. That's often left out of examples. Don't forget to also teach that you can join tables on two conditions (using AND/OR), not just one.

Thanks.

David Walker


i have seen the same thing

the rule is you write code to look cool, hardware is cheap


https://plus.google.com/100125998302068852885/posts?hl=en
http://twitter.com/alent1234
x-box live gamertag: i am null
[url=http://live.xbox.com/en-US/MyXbox/Profile[/url]
Post #799220
Posted Wednesday, October 7, 2009 8:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 12, 2011 3:42 PM
Points: 5, Visits: 28
I created this article/test page a number of years ago that describes Inner, Left, Right, Full, Cross, Triple, Self, Union and Union All queries. Joins


Post #799231
Posted Wednesday, October 7, 2009 8:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 24, 2011 7:50 AM
Points: 41, Visits: 83
SQL Noob (10/7/2009)
is there any performance difference doing a union all compared to a join when you need to get all the rows from two or more tables? i have a database where i do union all on some data in 20 tables or so when running a report and it seems to take a long time

I'm not certain I understand your question.

A UNION ALL combines the data from two similar sets into a single set with all members of each set.

A JOIN matches the data in one set with the data in another set.

Based on my current knowledge of SQL, I believe it might be theoretically possible to replicate a UNION ALL in a JOIN (although even in theory I think it would be dependent on your table structure), but it would be atypical and in all likelihood it would take longer to run than a straight "union all".

I have not tested this theory.

I'll admit, even as I attempt to consider an alternative that involves a JOIN, I still envision needing a UNION ALL in the query and a requirement for each record to have a unique id across all tables.
Post #799237
Posted Wednesday, October 7, 2009 8:53 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, July 17, 2014 10:56 AM
Points: 3,924, Visits: 1,607
Excellent article Wagner! A good revision of good SQL and old but basic stuff for any developer to be called a good developer. Thanks again.

SQL DBA.
Post #799241
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse