0% found this document useful (0 votes)
55 views

SQL Cartesian or Cross Joins: Syntax

The document discusses Cartesian or cross joins in SQL, which return the Cartesian product of records from two or more joined tables by matching every record from one table with every record from the other tables. An example demonstrates performing a Cartesian join on two tables, Customers and Orders, which returns every possible combination of records between the tables.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

SQL Cartesian or Cross Joins: Syntax

The document discusses Cartesian or cross joins in SQL, which return the Cartesian product of records from two or more joined tables by matching every record from one table with every record from the other tables. An example demonstrates performing a Cartesian join on two tables, Customers and Orders, which returns every possible combination of records between the tables.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL CARTESIAN OR CROSS JOINS

http://www.tuto rialspo int.co m/sql/sql-carte sian-jo ins.htm

Co pyrig ht tuto rials po int.co m

T he CART ESIAN J O IN or CRO SS J O IN returns the Cartesian product of the sets of records from the
two or more joined tables. T hus, it equates to an inner join where the join-condition always evaluates to T rue or
where the join-condition is absent from the statement.

Syntax:
T he basic syntax of INNER J O IN is as follows:
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]

Example:
Consider the following two tables, (a) CUST OMERS table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME
| AGE | ADDRESS
| SALARY
|
+----+----------+-----+-----------+----------+
| 1 | Ramesh
| 32 | Ahmedabad | 2000.00 |
| 2 | Khilan
| 25 | Delhi
| 1500.00 |
| 3 | kaushik | 23 | Kota
| 2000.00 |
| 4 | Chaitali | 25 | Mumbai
| 6500.00 |
| 5 | Hardik
| 27 | Bhopal
| 8500.00 |
| 6 | Komal
| 22 | MP
| 4500.00 |
| 7 | Muffy
| 24 | Indore
| 10000.00 |
+----+----------+-----+-----------+----------+

(b) Another table is ORDERS as follows:


+-----+---------------------+-------------+--------+
|OID | DATE
| CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 |
3 |
3000 |
| 100 | 2009-10-08 00:00:00 |
3 |
1500 |
| 101 | 2009-11-20 00:00:00 |
2 |
1560 |
| 103 | 2008-05-20 00:00:00 |
4 |
2060 |
+-----+---------------------+-------------+--------+

Now, let us join these two tables using INNER JOIN as follows:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS, ORDERS;

T his would produce the following result:


+----+----------+--------+---------------------+
| ID | NAME
| AMOUNT | DATE
|
+----+----------+--------+---------------------+
| 1 | Ramesh
|
3000 | 2009-10-08 00:00:00 |
| 1 | Ramesh
|
1500 | 2009-10-08 00:00:00 |
| 1 | Ramesh
|
1560 | 2009-11-20 00:00:00 |
| 1 | Ramesh
|
2060 | 2008-05-20 00:00:00 |
| 2 | Khilan
|
3000 | 2009-10-08 00:00:00 |
| 2 | Khilan
|
1500 | 2009-10-08 00:00:00 |
| 2 | Khilan
|
1560 | 2009-11-20 00:00:00 |
| 2 | Khilan
|
2060 | 2008-05-20 00:00:00 |
| 3 | kaushik |
3000 | 2009-10-08 00:00:00 |
| 3 | kaushik |
1500 | 2009-10-08 00:00:00 |
| 3 | kaushik |
1560 | 2009-11-20 00:00:00 |
| 3 | kaushik |
2060 | 2008-05-20 00:00:00 |
| 4 | Chaitali |
3000 | 2009-10-08 00:00:00 |

| 4 | Chaitali |
1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali |
1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali |
2060 | 2008-05-20 00:00:00 |
| 5 | Hardik
|
3000 | 2009-10-08 00:00:00 |
| 5 | Hardik
|
1500 | 2009-10-08 00:00:00 |
| 5 | Hardik
|
1560 | 2009-11-20 00:00:00 |
| 5 | Hardik
|
2060 | 2008-05-20 00:00:00 |
| 6 | Komal
|
3000 | 2009-10-08 00:00:00 |
| 6 | Komal
|
1500 | 2009-10-08 00:00:00 |
| 6 | Komal
|
1560 | 2009-11-20 00:00:00 |
| 6 | Komal
|
2060 | 2008-05-20 00:00:00 |
| 7 | Muffy
|
3000 | 2009-10-08 00:00:00 |
| 7 | Muffy
|
1500 | 2009-10-08 00:00:00 |
| 7 | Muffy
|
1560 | 2009-11-20 00:00:00 |
| 7 | Muffy
|
2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+

You might also like