SQL Subqueries in FROM clause


Example

You can use subqueries to define a temporary table and use it in the FROM clause of an "outer" query.

SELECT * FROM (SELECT city, temp_hi - temp_lo AS temp_var FROM weather) AS w
WHERE temp_var > 20;

The above finds cities from the weather table whose daily temperature variation is greater than 20. The result is:

citytemp_var
ST LOUIS21
LOS ANGELES31
LOS ANGELES23
LOS ANGELES31
LOS ANGELES27
LOS ANGELES28
LOS ANGELES28
LOS ANGELES32

.