Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does SELECT from two tables separated by a comma work? (SELECT * FROM T1, T2)

Tags:

sql

database

People also ask

How do you use comma separated values in SQL?

Now for the perfect comma separated value we must remove the first comma in the above query result. This can be done using "STUFF" or the SUBSTRING function. STUFF deletes a specified length of characters from the given string and inserts another set of characters at a specified starting point.

What does SELECT * from table return?

An asterisk (" * ") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from.

Can you SELECT from two tables?

A simple SELECT statement is the most basic way to query multiple tables. You can call more than one table in the FROM clause to combine results from multiple tables. Here's an example of how this works: SELECT table1.


The comma between the two tables signifies a CROSS JOIN, which gives the Cartesian product of the two tables. Your query is equivalent to:

SELECT *
FROM T1
CROSS JOIN T2

The result is every pairing of a row from the first table with a row from the second table. The number of rows in the result is therefore the product of the number of rows in the original tables. In this case the answer is 3 x 3 = 9.

The rows will be as follows:

T1.foo   T2.bar
A        1
A        2
A        3
B        1
B        2
B        3
C        1
C        2
C        3