tik
tik

Reputation: 135

Select from two tables which have no relation

I don't know if it is possible or not. If possible, please help me to resolve this.

I have two tables, table1 and table2:

        table1                                table2
 column1     column2                column3            column4      
    1           2                      A                   B              
    3           4                      C                   D

There is no relation between table1 and table2. I want to execute a query so that my output looks like this:

Output table:

  column1      column2       column3       column4
     1            2             A             B
     1            2             C             D
     3            4             A             B
     3            4             C             D

Can anyone please tell me how can I achieve this? We are using SQL Server 2005.

Thanks, Kartic

Upvotes: 3

Views: 6079

Answers (3)

Ondra Žižka
Ondra Žižka

Reputation: 46806

Like this?

SELECT * FROM table1 CROSS JOIN table2

It's called a cross join, or cartesian product.

You can add additional filtering or join conditions using WHERE.

Upvotes: 0

Flying_Machine
Flying_Machine

Reputation: 323

You need to use cross join.

select t1.*,t2.* from table1 t1
Cross Join table2 t2;

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269953

This is called a cross join, which produces a Cartesian product of all the records in each of the tables. The best way to do this is explicitly, with the cross join syntax:

select t1.*, t2.*
from table1 t1 cross join
     table2 t2;

Note that if either table is empty, then you will not get any rows back.

Upvotes: 2

Related Questions