SELECT (SELECT SUM(table1.col1) FROM table1) AS sum_1, (SELECT SUM(table2.col1) FROM table2) AS sum_2;
You can also write it as:
SELECT t1.sum_c1, t1.sum_c2, t2.sum_t2_c1 FROM ( SELECT SUM(col1) sum_c1, SUM(col2) sum_c2 FROM table1 ) t1 FULL OUTER JOIN ( SELECT SUM(col1) sum_t2_c1 FROM table2 ) t2 ON 1=1;
FULL JOIN is used with the dud condition, so any subquery cannot give any results (empty), without causing a larger query to fail.
I don’t think that the query you wrote would give the result you expected to receive, because it makes CROSS JOIN between table1 and table2, which inflates each SUM by the number of rows in another table. Note that if table1 / table2 is empty, CROSS JOIN will cause X rows to 0 rows to return an empty result.
Take a look at SQL Fiddle and compare the results.
RichardTheKiwi
source share