Memra

Subqueries

A query inside a query.

Queries within queries

A subquery is a SELECT nested in another. A *scalar* subquery returns one value you can compare against — "books priced above average":

SELECT title, price
FROM books
WHERE price > (SELECT AVG(price) FROM books);

A subquery can also feed an IN (...) with a whole column of values — "books by UK authors":

SELECT title FROM books
WHERE author_id IN (SELECT id FROM authors WHERE country = 'UK');
NORMAL ~/memra/learn/postgresql/subqueries utf-8 LF