Questions tagged [cross-join]

For any question involving a CROSS JOIN, i.e. combining every SELECT-ed field from one table with every SELECT-ed field from another.

A CROSS JOIN is part of the ISO/ANSI SQL Standard and occurs when 2 tables are JOIN-ed with no ON criteria specified. This is the equivalent of combining every SELECT-ed field of table 1 with every SELECT-ed field of table 2.

Example:

CREATE TABLE t1 (x INT NOT NULL);
INSERT INTO t1 VALUES (1), (2), (3);

and

CREATE TABLE t2 (y TEXT NOT NULL);
INSERT INTO t2 VALUES ('a'), ('b'), ('c');

Now, we perform the CROSS JOIN. There are two ways of doing this - the "preferred" one is:

SELECT t1.x, t2.y
FROM t1
CROSS JOIN t2;

Result: x y 1 a 1 b 1 c 2 a 2 b 2 c 3 a 3 b 3 c 9 rows

An alternative way of writing this is:

SELECT t1.x, t2.y
FROM t1, t2;

Result:

idem

Some RDBMS's support a third syntax - ON TRUE.

SELECT t1.x, t2.y
FROM t1
JOIN t2
ON TRUE;

Result:

idem

A fiddle of the above is available here.

15 questions
5
votes
2 answers

Is there such a thing as a LEFT CROSS JOIN?

Is there a way to do what I want to call a "LEFT CROSS JOIN"? I.e., is there a way to specify a CROSS JOIN ON, but return at least one row in the result for every row in the left, using nulls for the right-side columns when the right-side table does…
Joshua Goldberg
  • 227
  • 2
  • 6
4
votes
2 answers

If LATERAL is optional for table-valued functions, then why does this query error without it?

Setup CREATE TABLE persons ( person_id int not null, name TEXT ); INSERT INTO persons VALUES (1, 'Adam'), (2, 'Paul'), (3, 'Tye'), (4, 'Sarah'); CREATE TABLE json_to_parse ( person_id int not null, block json ); INSERT INTO json_to_parse…
J. Mini
  • 1,161
  • 8
  • 32
3
votes
1 answer

How do I create a SQL loop that generates future dates based on different frequencies and intervals for each item until a specified end date?

I have a data set that appears like this: next_generation_date procedure interval frequency 2021-01-17 00:00:00.000 Clean Restroom 1 day 2021-01-17 00:00:00.000 Vacuum 2 week 2021-02-01 00:00:00.000 Inspect Fire…
Gulfhawk
  • 31
  • 1
  • 2
2
votes
1 answer

SQL Server JOIN vs sparse table

I am in the need to query a relation between an entity called article (it's a product sold in a shop) and it's characterstics (a set of attributes). The result will always presented in a paged manner (so only few articles record are read each…
1
vote
1 answer

Pairing Query Store Read/Write Status with Enabled Status

I am able to find if Query Store is enabled: SELECT name, is_query_store_on from sys.databases I am able to query a specific database to determine the Query Store Read/Write Status: SELECT actual_state_desc from…
CrushingIT
  • 33
  • 4
0
votes
0 answers

PostgreSQL query optimization over multiple tables with moderately large data

We have a data structure in PostgreSQL 16 which is moderately complex, I have a simplified diagram showing the join columns here: The 3 SURVEY tables on the top left are exceptions because they are not joined with foreign key, rather we have a…
Gábor Major
  • 163
  • 1
  • 7
0
votes
3 answers

Is it a good idea to join data from two different databases?

I am building an application that requires a lot of tables in one database and while the joining and aggregation of data is really nice and seamless, I am starting to wonder if I am creating too many tables in one database rather than organizing…
jaffer_syed
  • 13
  • 1
  • 2
0
votes
1 answer

Is it possible to sort rows by values from generate_series()?

I multiply result rows of a SELECT with CROSS JOIN generate_series(1, max). My query is like: select id, name from person CROSS JOIN generate_series(1, 4) where ; I have this…
0
votes
0 answers

Joining Two Tables via JSONB column (date)

Given the following data: create table datas (id int, data jsonb); insert into categories (id, budget) values (1, '{"2022-07-01": {"budget_amount": value1, "some": "thing1"},"2022-08-01": {"budget_amount": value2, "some": "thing2"}}'), (2,…
RMcLellan
  • 43
  • 5
0
votes
0 answers

How to show all the columns data in full outer join

I have 2 tables. The Result is Issue is EnteredBy=23 is not displaying because I am using CT1.EnteredBy. How to display CT11.EnteredBy value instead of NULL The Actual Query I am using is given below With CT1 As( Select…
Diya Rawat
  • 23
  • 4
0
votes
1 answer

Using the correct JOIN

I am trying to create an app for the salespeople who work for my company to keep track of their sales/commission When a salesperson brings on an account, their name is noted on the account and the table might look like Table…
PaulMcF87
  • 177
  • 6
0
votes
1 answer

Doesn't T-SQL support correlated CROSS JOINs?

This query works SELECT QMC.HAUSKEY, t1.STRNAME, t2.HAUS_NR FROM SWOPS.MIGR.QMCAddresses QMC CROSS JOIN (SELECT STRNAME FROM SWOPS.MIGR.EB_DICT_STREET_QMC WHERE FK=8055909) t1 CROSS JOIN (SELECT HAUS_NR FROM…
0
votes
0 answers

Pivot table but with cross product

I' currently in the following situation: I have a measurements table that looks something like this: +----+-----------+-------+---------+ | id | germplasm | trait | m_value | +----+-----------+-------+---------+ | 1 | g1 | t1 | 1…
Baz
  • 165
  • 1
  • 1
  • 9
0
votes
0 answers

Is there a cross join in these CTEs?

I have a query that is constructed from several CTEs (in order to organize the query). I constructed it in my SQL workbench and was happy with the test results on a smaller dataset, so I converted it to SQLalchemy code for executing it on the…
Technaton
  • 171
  • 1
  • 1
  • 4
0
votes
1 answer

Add a list of names to a generated series

I generated a date series this way: SELECT generate_series(date '2020-01-01', date '2021-01-21', '1 day'); Additionally I have a list with name (Max Meyer, Anna Smith, Peter Gardner). How could I add the name list to the date series, such that the…
wladi
  • 1
  • 1