I wonder if there any case that a join can not be converted to a sub-query.
BTW: I am not discussing advantages and disadvantage of using sub-query or using join.
I wonder if there any case that a join can not be converted to a sub-query.
BTW: I am not discussing advantages and disadvantage of using sub-query or using join.
As far as I know, a JOIN [INNER JOIN]
, from the theoretical point of view, is a projection of a CROSS JOIN
(every combination of two tables). If you can get a cross join using a subquery and apply any function on it, then you have a perfect substitute. I think you can always transform:
SELECT T1.A, T2.B
FROM T1
JOIN T2
ON condition(T1.C, T2.D)
into:
SELECT T1.A, T2.B
FROM T1
WHERE condition(T1.C, ALL(SELECT T2.D FROM T2))
A similar conclusion can be reached with LEFT [OUTER] JOINS
and RIGHT [OUTER] JOINS
(the other types supported on MySQL), by including the NULL value:
SELECT T1.A, T2.B
FROM T1
WHERE condition(T1.C, ALL(SELECT T2.D FROM T2) UNION SELECT NULL)
I would say the statement is false, though it's difficult to prove a negative.
I don't believe a CROSS JOIN
could be done with sub queries
SELECT *
FROM T1 CROSS JOIN T2
or for that matter any join that delivers a many to many result
SELECT c , (SELECT i FROM t2 LIMIT 1) as i FROM t1 UNION SELECT c , (SELECT i FROM t2 LIMIT 1, 1) as i FROM t1 UNION SELECT c, (SELECT i FROM t2 LIMIT 2, 1) FROM t1 ...;