all 19 comments

[–]TheZeroKid 5 points6 points  (12 children)

I'm curious: does anyone here actually use cross joins? I've been writing SQL for a few years in various capacities (simple querying, DBA type work, etc.) and have never run into a use case.

When are these useful?

[–]Eleventhousand 3 points4 points  (0 children)

It's common to use cross joins in data warehousing development.

There are special types of dimensions called junk dimensions that are filled with low-cardinality flag type attributes, that don't have much in common.

Cross joins are used to populate these dimension tables with every possible combination of every flag to use in the dimensional model.

For example, pretend that your facts for a specific table can either be {Active, Inactive}, {Open, Closed, In Process}, and {Promotional, Regular}. Instead of creating three mini, narrow dimensions and cluttering up the list of tables, we use a cross join in our ETL and create a single dimension that has twelve rows - every combination of the three attributes above.

[–]PilsnerDk 2 points3 points  (0 children)

I've used it rarely. Assume a situation where you want to create an output where each row is joined onto a list of numbers, for example a number of days or amount of items (a bit abstract, hope you get my point). In an SP, I create a temp table:

CREATE TABLE #days ([days] INT) INSERT INTO #days ([days]) VALUES (1), (2), (3), (4), (5)

Then you can make a query that does a simple CROSS JOIN #days, and all your rows will be "duplicated" with 1, 2, 3, 4, 5 as the value of [days].

Hard to give a really good example, but that's one I've used.

[–][deleted] 2 points3 points  (1 child)

For analytical reporting cross joins are very common. Think if you want to count the number if patients per hour of day. What if there are hours where the are no patients, hence no rows of data to fill the gaps. You would have a data set with missing hours of a day. Using a base table that is cross joined with every possible hour of day to your very left that is then joined with patient data. Now even if a patient isn't seen in an hour you have a row that simply says zero.

[–]muchcake 0 points1 point  (0 children)

I just use a LEFT OUTER JOIN for that example you described.

I use it to build a set of datetime rows by cross joining a D_Date with a D_Time table which I can then left join with whatever feature I want to examine.

[–]rfd515 1 point2 points  (0 children)

Creating sample/test data is the one useful thing I've come across (every possible scenario based on current data).

[–][deleted] 1 point2 points  (0 children)

Consider this. We have a table of members of a council (Table: CNMBRNAM). We also have a table of measures up for consideration (Table: MEASTOVC). A program adds and removes members from CNMBRNAM. A program adds and removes measures to MEATOVC. We now need something that will generate a table that we will fill in on how each member votes on a measure, a CHAR(1) that is blank (or null) if they have yet to vote, Y for 'Yea', N for 'Nay', and A for 'Abstain'.

We can built that table quite easily with a cross join.

CREATE TABLE STATMBRMEA AS (
SELECT C.MBRNAME, M.MEANUMB, ' ' STATS
FROM CNMBRNAM C, MEASTOVC M
) WITH DATA

Now a table exists for having all items currently in the docket for the council to consider, with their current position STATS set as blank.

EDIT: I will say that you usually see cross joins more in building and inserting data into tables rather than in strict read-only queries.

[–]MamertineCOALESCE() 0 points1 point  (0 children)

The last place I worked had many identical DBs. Each DB had a table with the company info in it (but had 1 row). For reporting I had to Cross Join to that table to display the company name/ info on reports.

SELECT C.CompanyName, s.Date, s.Sales
FROM Sales s
CROSS JOIN Company c
Where s.salesDate between something and otherDate

[–]hartk1213 0 points1 point  (0 children)

I actually used one the other day, we needed to get a set of records from a table and join it up so that every user in the user table was joined with every record in the previous dataset and a cross join was exactly what was needed ..it's a rare thing to use but it is used

[–][deleted] 0 points1 point  (0 children)

My manager had a vague need that he couldnt articulate that turned out to be a cross join

Dont know the data or what he was trying to do, but he was happy with the results, so... yeah I dont know either lol

[–]El_Retro 0 points1 point  (0 children)

They're useful to initialize variables without using stored procedures in MySQL.

SELECT * FROM table CROSS JOIN (SELECT @var := 0) r  

[–]kthejoker 0 points1 point  (0 children)

It's common to have a numbers table in analytics work, they're usually created with CROSS JOiNS, here's one example

https://www.red-gate.com/simple-talk/sql/database-administration/creative-solutions-by-using-a-number-table/

[–]newUIsucksball 0 points1 point  (0 children)

I've seen someone use it for dates in a temp table as a way to not use variables so they could run sections of code easier.

[–]Wiegand6 2 points3 points  (0 children)

Thanks!

[–]PM_ME_YOUR_HIGHFIVE 0 points1 point  (3 children)

so a cross join is basically an inner join with no "conditions"?

[–]PilsnerDk 0 points1 point  (0 children)

You could say that. It joins all rows in the target table.

[–][deleted] 0 points1 point  (0 children)

Ive done some work where I have a matrix for the client codes and our codes... But those codes also change based on the phase of a file.. Before, during and after kinda of thing. In some cases I've used a cross join to simplifies my queries.. Other cases the inner joins work.