Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from Table1, Table2

Tags:

sql

select

I found the following query and appreciate it if someone can help explain to me what this means.

select * from table1, table2
like image 770
Asiri Dissanayaka Avatar asked Oct 04 '16 18:10

Asiri Dissanayaka


People also ask

What is select * from table1 table2?

It produces a cartesian product, so the number of rows in the result set will be the number of rows from table1 multiplied by number of rows from table2 (assuming there are no constraints in the WHERE clause). It effectively pairs each row from table1 with a row coming from table2 .

What is a SQL view and what is it used for?

A VIEW in SQL Server is like a virtual table that contains data from one or multiple tables. It does not hold any data and does not exist physically in the database. Similar to a SQL table, the view name should be unique in a database. It contains a set of predefined SQL queries to fetch data from the database.

What is full outer join in SQL with example?

Note: The FULL OUTER JOIN keyword returns all matching records from both tables whether the other table matches or not. So, if there are rows in "Customers" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Customers", those rows will be listed as well.


1 Answers

This is called CROSS JOIN but using old syntax with , in FROM clause. My advice is not to use old syntax, stick with the JOIN here.

It produces a cartesian product, so the number of rows in the result set will be the number of rows from table1 multiplied by number of rows from table2 (assuming there are no constraints in the WHERE clause). It effectively pairs each row from table1 with a row coming from table2.

Below query is an equivalent but does explicit JOIN operation which separates constraint logic of data retrieval (normally put within the WHERE clause) from logic of connecting related data stored across separate tables (within the FROM clause):

SELECT *
FROM table1
CROSS JOIN table2

Consider an example where table1 has 8 rows and table2 has 5 rows. In the output, you get 40 rows (8 rows * 5 rows), because it pairs all rows from both sources (tables).

like image 130
Kamil Gosciminski Avatar answered Sep 19 '22 03:09

Kamil Gosciminski