what operation does "select from table1, table2 " imply? [duplicate] - sql

This question already has answers here:
Select from Table1, Table2
(3 answers)
Closed 4 years ago.
I know different joins, but I wanted to know which of them is being used when we run queries like this:
select * from table1 t1, table2 t2
is it full outer join or natural join for example?
Also does it have a unique meaning among different databases or all do the same?
UPDATE: what if we add where clause ? will it be always inner join?

The comma in the from clause -- by itself -- is equivalent to cross join in almost all databases. So:
from table1 t1, table2 t2
is functionally equivalent to:
from table1 t1 cross join table2 t2
They are not exactly equivalent, because the scoping rules within the from clause are slightly different. So:
from table1 t1, table2 t2 join
table3 t3
on t1.x = t3.x
generates an error, whereas the equivalent query with cross join works.
In general, conditions in the WHERE clause will always result in the INNER JOIN. However, some databases have extended the syntax to support outer joins in the WHERE clause.
I can think of one exception where the comma does not mean CROSS JOIN. Google's BigQuery originally used the comma for UNION ALL. However, that is only in Legacy SQL and they have removed that in Standard SQL.
Commas in the FROM clause have been out of fashion since the 1900s. They are the "original" form of joining tables in SQL, but explicit JOIN syntax is much better.
To me, they also mean someone who learned SQL decades ago and refused to learn about outer joins, or someone who has learned SQL from ancient materials -- and doesn't know a lot of other things that SQL does.

demo: db<>fiddle
This is a CROSS JOIN (cartesian product). So both of the following queries are equal
SELECT * FROM table1, table2 -- implicit CROSS JOIN
SELECT * FROM table1 CROSS JOIN table1 -- explicit CROSS JOIN
concerning UPDATE
A WHERE clause makes the general CROSS JOIN to an INNER JOIN. An INNER JOIN can be got by three ways:
SELECT * FROM table1, table2 WHERE table1.id = table2.id -- implicit CROSS JOIN notation
SELECT * FROM table1 CROSS JOIN table2 WHERE table1.id = table2.id -- really unusual!: explicit CROSS JOIN notation
SELECT * FROM table1 INNER JOIN table2 ON (table1.id = table2.id) -- explicit INNER JOIN NOTATION
Further reading (wikipedia)

Related

SQL: How to use WHERE instead of OUTER JOIN

I am using an old database call Centura Gupta that doesn't have the join clauses (left join, right join, inner join, outer join). So I need to use where to replace the outer join that I need:
SELECT *
FROM table1
OUTER JOIN table2
ON table1.id_person table2.id_person;
I think that where can only replaces inner join:
SELECT *
FROM table1
WHERE table1.id_person = table2.id_person;
You could try to use subqueries instead of join
SELECT *
FROM table1 t1
WHERE id_Person IN
(
SELECT id_Person
FROM table2
)
I used this kind of implementation when I didn't know JOINs. May not be exactly right but something which can get you close:
SELECT *
FROM table1 t1, table2 t2
WHERE t1.id_Person=t2.id_Person;
I don't know about that particular database, but you might be able to use a correlated subquery to get "joined" data. This will pull all records from table1 and the related info from table2, or NULL for whatever4 and whatever5 if there's no matching id_person in table2:
SELECT t1.whatever1
, t1.whatever2
, t1.whatever3
, (SELECT whatever4 FROM table2 AS t2 WHERE t2.id_person = t1.id_person) AS whatever4
, (SELECT whatever5 FROM table2 AS t2 WHERE t2.id_person = t1.id_person) AS whatever5
FROM table1 AS t1
'Old database called Centura Gupta' ??
Maybe you actually mean OpenText Gupta SQLBase - which is by no means 'old'.
You may be running an 'old' version of SQLBase - but it is now up to v12.2 native 64bit, and outer joins are certainly supported.
You can either use the native Gupta outer joins - similar to Oracle (+) or standard ANSI outer joins.
If you want to use ANSI OUTER joins, specify 'ANSIJoinSyntax=1' in your Server side Sql.ini
Go here for more SQLBase Join syntax: Gupta SQLBase Manuals
Native Gupta Outer Join:
SELECT t1.id_person, t2.id_person
From table1 t1 , table t2
Where t1.id_person = t2.id_person(+)
ANSI Outer Join:
SELECT t1.person_id, t2.person_id
From table1 t1
Left Outer Join table2 t2 ON t1.id_person = t2.id_person
Where <what ever filter you want>
Go here for more SQLBase Join syntax: Gupta SQLBase Manuals

Can the SQL JOIN 'USING()' clause be used with one or more 'AND ON()' clauses for a single join..?

In a DB2-400 SQL join, can the USING() clause be used with one or more AND ON clauses for a single join..? This is for a situation where some field names are the same, but not all, so USING() would only apply to part of the join.
I could have sworn I've done this before and it worked, but now it eludes me.
I've tried various combinations as shown below, but none of them work. Perhaps I'm simply mistaken and it's not possible:
SELECT * FROM T1 INNER JOIN T2 USING (COL1,COL2) AND ON (T1.COL3=T2.COL4)
SELECT * FROM T1 INNER JOIN T2 ON (T1.COL3=T2.COL4) AND USING (COL1,COL2)
SELECT * FROM T1 INNER JOIN T2 ON (T1.COL3=T2.COL4), USING (COL1,COL2)
SELECT * FROM T1 INNER JOIN T2 USING (COL1,COL2,(1.COL3=T2.COL4))
Checking the syntax diagram here https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/db2/rbafzjoinedt.htm
I suggest that the only options for JOIN USING are a comma separated list of columns
JOIN table-reference USING ( column-name [, column-name] ... )
and you can't mix USING with ON
You can use where:
SELECT *
FROM T1 INNER JOIN
T2 USING (COL1, COL2)
WHERE T1.COL3 = T2.COL4;
Another alternative would be to use a subquery to rename the column in one of the tables.

SQL Inner Join with no WHERE clause

I was wondering, how does an inner join work when no WHERE clause is specified? For example,
SELECT table1.letter, table2.letter, table1.number, table2.number
FROM tbl AS table1, tbl AS table2;
tbl:
text, integer
a , 1
b , 2
c , 3
Tried finding some examples online but I couldn't seem to find any :-/
Thanks!
The current implicit join syntax you are using:
FROM tbl AS table1, tbl AS table2;
will result in a cross join if no restrictions are present in the WHERE clause. But really you should use modern ANSI-92 syntax when writing your queries, e.g.
SELECT
table1.letter,
table2.letter,
table1.number,
table2.number
FROM tbl AS table1
INNER JOIN tbl AS table2
-- ON <some conditions>
One obvious reason to use this syntax is that it makes it much easier to see the logic of your query. In this case, if your updated query were missing an ON clause, then we would know right away that it is doing a cross join, which most of the time is usually not what you want to be doing.
The comma operator generates a Cartesian product -- every row in the first table combined with every row of the second.
This is more properly written using the explicit cross join:
SELECT table1.letter, table2.letter, table1.number, table2.number
FROM tbl table1 CROSS JOIN
tbl table2;
If you have conditions for combining the two tables, then you would normally use JOIN with an ON clause.
You can use cross join
select * from table1 cross join table2
Here is a link to understand more about the use of cross join.
https://www.w3resource.com/sql/joins/cross-join.php

How to simplify to use ISNULL with LEFT OUTER JOIN to merge tables

I need to merge below Table 1 & Table 2 as indicated in "Merge".
The query I wrote below works; but it is TOO SLOW! Is there any simpler way which returns it faster?
SELECT T1.Loc, T2.SO, T2.PO, T1.Item
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON ISNULL(T1.[SO2],T1.[SO1]) = T2.[SO]
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax:
SELECT *
FROM Table1 T1 LEFT OUTER JOIN
Table2 T2
ON COALESCE(T1.[SO2], T1.[SO1]) = T2.[SO];
There is no need to join to Table2 twice (much less getting a Cartesian product). I prefer COALESCE() to ISNULL() simply because the former is the ANSI standard function for replacing NULL values.
I should add that for performance, two joins might be preferable:
SELECT T1.Loc, COALESCE(T2_2.SO, T2_1.SO) as SO,
COALESCE(T2_2.PO, T2_1.PO) as PO, T1.Item
FROM Table1 T1 LEFT OUTER JOIN
Table2 T2_2
ON T1.[SO2] = T2_2.[SO] LEFT OUTER JOIN
Table2 T2_1
ON T1.[SO1] = T2_1.[SO] AND T1.[SO2] IS NULL;
It is easier for the SQL engine to optimize joins that use only equality and AND conditions, on the clean column references (that is, the columns are not the arguments to any function calls).
Hope this SQL full fill your requirement.
Select T2.Loc, T2.SO, T2.PO, T1.item from table 2 T2 join table1 T1
on T1.SO1 = T2.SO;

Postgres/netezza multiple join from multiple tables

Hi I have a problem when migrating from ORACLE to Netezza, netezza seems to have problem if multiple tables is declared before using JOIN`s. How could I write this join differently ?
INSERT INTO...
SELECT...
FROM table1 t1, table2 t2 //here seems to be the problem as postgres dont allow to put two tables in FROM clause if there are JOIN`s involved
JOIN talbe3 t3 ON t2.column = t3.column
JOIN table4 t4 ON t2.column = t4.column
LEFT OUTER JOIN table5 t5 ON (t4.column=t5.column AND t4.column=t2.column AND t4.column=t3.column)
WHERE....;
You simply should not mix old-style (implicit) and new-style (explicit) joins. In fact, a simple rule is simply to avoid commas in the from clause.
I imagine the problem that you have is a scoping problem for the table aliases. I know this happens in MySQL. But, because I never use commas in from clauses, I am not aware of how this affects other databases. I think the part of the from clause after the comma is parsed as a unit, and the aliases defined before are not known during this parsing stage.
In any case, whatever the problem, the simple solution is to replace the comma with CROSS JOIN:
INSERT INTO...
SELECT...
FROM table1 t1 CROSS JOIN table2 t2 //here seems to be the problem as postgres dont allow to put two tables in FROM clause if there are JOIN`s involved
JOIN table3 t3 ON t2.column = t3.column
JOIN table4 t4 ON t2.column = t4.column
LEFT OUTER JOIN table5 t5 ON (t4.column=t5.column AND t4.column=t2.column AND t4.column=t3.column)
WHERE....;
This should work in all the databases you mention -- and more.