CodeBlanks Logo Icon CodeBlanks

ANY and ALL

Compare against a subquery with ANY and ALL.

ANY and ALL

ANY compares against any value in the list, ALL against every value.

SELECT name FROM products
WHERE price > ANY (SELECT price FROM products WHERE category = 'Kitap');

This query returns products more expensive than any book.

name is selected from products. WHERE keeps only the rows where price > ANY (SELECT price FROM products WHERE category = … holds.

ANY and ALL — details

ANY means 'at least one', ALL means 'every'. They work with the list from the subquery.

For example, price > ALL (…) returns values greater than the largest in the list, while price > ANY (…) returns those greater than the smallest.

Practice ANY and ALL

Find products more expensive than any book using > ANY (SELECT price ...).

Starter code

query.sql
-- more than any
SELECT name FROM products
WHERE price >

Sample solution

query.sql
SELECT name FROM products
WHERE price > ANY (SELECT price FROM products WHERE category = 'Kitap');

Expected output

name
Laptop
Telefon

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Add ANY

Question
SELECT name FROM products
WHERE price > ____ (SELECT price FROM products WHERE category = 'Kitap');
Answer
SELECT name FROM products
WHERE price > ANY (SELECT price FROM products WHERE category = 'Kitap');

Explanation: > ANY returns rows greater than at least one value from the subquery.

2 What does ALL mean?

  • Everyone Correct answer
  • Any one of them
  • Exactly one
  • None

Explanation: ALL compares against every value.

3 ANY and ALL: match the terms

Term Meaning
ANY any
ALL all
IN list
EXISTS existence

All lessons in Intermediate