Home / code / sql

SQL subqueries

With a subquery, the results of a select-instruction is used in another select-instruction.

SELECT fields FROM table WHERE field1=(SELECT max(field1) FROM table)
The result is one record with the max of field1.
With the operators <, >, <=, >=, = the subquery gives only 1 value as result.

If you want a list, you'll need to use IN.
SELECT fields FROM table WHERE field1 IN (SELECT max(field1) FROM table)
The result is a list of all record with the max of field1.

 

TOP

Latest script:

 

Books: