July 2010
Intermediate to advanced
840 pages
16h 33m
English
Consider a three-table JOIN like this.
SELECT P1.paint_color
FROM Paints AS P1, Warehouse AS W1, Sales AS S1
WHERE W1.qty_on_hand + S1.qty_sold =
P1.gallons/2.5;Because all of the columns involved in the JOIN are in a single expression, their indexes cannot be used. The SQL engine will construct the CROSS JOIN of all three tables first and then prune that temporary working table to get the final answer. In Standard SQL, you can first do a subquery with a CROSS JOIN to get one side of the equation:
(SELECT (W1.qty_on_hand + S1.qty_sold) AS stuff FROM Warehouse AS W1 CROSS JOIN Sales AS S1)
and then push it into the WHERE clause, like this:
SELECT color FROM Paints AS P1 WHERE EXISTS ((SELECT (W1.qty_on_hand + S1.qty_sold) ...
Read now
Unlock full access