SQL: Subquery

20th August 2020 at 2:19pm
SQL

子查询结果在 where 语句中做相等判断

-- Wrong: Aggregate functions can not be used in where clause
SELECT city FROM weather WHERE temp_lo = max(temp_lo);

-- Wrong
SELECT city FROM weather
    WHERE temp_lo = (SELECT temp_lo FROM weather);

ERROR:  more than one row returned by a subquery used as an expression

-- Right
SELECT city FROM weather
    WHERE temp_lo = (SELECT max(temp_lo) FROM weather);

     city      
---------------
 San Francisco
(1 row)