Why are 9 rows extracted from this query?

Given 2 tables T1 and T2.
T1  T2 
A   1 
B   2
C   3
You make a query SELECT * FROM T1, T2.
What is the no: of rows that are fetched from this query? 

Answer 9

+3
source share
4 answers

This query leads to a Cartesian product , since no other conditions are provided. Each row from the first table is mapped to each row from the second table.

Result

A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
+10
source

Since each record from the first table is returned along with each record of the second table, and the result is not filtered.

The exact output will be:

T1  T2 
A   1 
A   2
A   3 
B   1
B   2
B   3
C   1
C   2
C   3

(order may vary)

+3
source

: (3) (3) , 3 * 3 = 9.

+2

This is what you requested. You got all rows from T1and all rows from T2. They are not just combined with each other - it will not work if the columns are different, for example, although you can do it with UNION- they are combined into what is called a “Cartesian product”. You essentially get all row combinations from both tables. And 3*3 = 9.

0
source

Source: https://habr.com/ru/post/1785420/


All Articles