Cross Join with itself : Cross Join « Table Join « SQL Server / T-SQL Tutorial






4>
5> CREATE TABLE Candidates(
6> candname varchar(10) NOT NULL,
7> gender   char(1)     NOT NULL CONSTRAINT CHK_gender CHECK (gender IN('F', 'M'))
8> )
9> INSERT INTO Candidates VALUES('A', 'M')
10> INSERT INTO Candidates VALUES('B', 'M')
11> INSERT INTO Candidates VALUES('C', 'F')
12> INSERT INTO Candidates VALUES('D', 'F')
13>
14> SELECT
15>   T1.candname,
16>   T2.candname
17> FROM
18>     Candidates AS T1
19>   CROSS JOIN
20>     Candidates AS T2
21> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
candname   candname
---------- ----------
A          A
B          A
C          A
D          A
A          B
B          B
C          B
D          B
A          C
B          C
C          C
D          C
A          D
B          D
C          D
D          D

(16 rows affected)
1>
2> drop table candidates
3> GO








4.2.Cross Join
4.2.1.SQL-92 Cross Join Syntax
4.2.2.Cross Join with itself
4.2.3.Matching Couples Using a Cross Join; Couples with Different Genders
4.2.4.it is the Cartesian product of all the rows from all tables participating in the SELECT statement