Bird
Raised Fist0
PostgreSQLquery~20 mins

ALL, ANY, SOME with subqueries in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ALL, ANY, SOME Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of ALL with subquery
Given a table products with columns id, price, and category, what is the output of this query?
SELECT id FROM products WHERE price > ALL (SELECT price FROM products WHERE category = 'Books');
PostgreSQL
SELECT id FROM products WHERE price > ALL (SELECT price FROM products WHERE category = 'Books');
AReturns IDs of products priced higher than at least one product in the 'Books' category
BReturns IDs of products priced lower than every product in the 'Books' category
CReturns IDs of products priced higher than every product in the 'Books' category
DReturns IDs of products priced equal to the highest price in the 'Books' category
Attempts:
2 left
💡 Hint
ALL means the condition must be true for every value returned by the subquery.
❓ query_result
intermediate
2:00remaining
Output of ANY with subquery
Consider a table employees with columns id, salary, and department. What does this query return?
SELECT id FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE department = 'Sales');
PostgreSQL
SELECT id FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE department = 'Sales');
AEmployees with salary less than all employees in Sales
BEmployees with salary less than at least one employee in Sales
CEmployees with salary equal to the lowest salary in Sales
DEmployees with salary greater than all employees in Sales
Attempts:
2 left
💡 Hint
ANY means the condition must be true for at least one value from the subquery.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this ALL query
Which option contains a syntax error when using ALL with a subquery in PostgreSQL?
PostgreSQL
SELECT id FROM orders WHERE amount > ALL SELECT amount FROM orders WHERE status = 'completed';
AMissing parentheses around the subquery after ALL
BUsing ALL instead of ANY causes syntax error
CSubquery must use DISTINCT keyword with ALL
DALL cannot be used with numeric columns
Attempts:
2 left
💡 Hint
Check if the subquery is properly enclosed.
❓ optimization
advanced
2:00remaining
Optimizing ANY with subquery for performance
You have a large sales table with columns id, region, and amount. Which query is more efficient to find sales with amount less than any sale in region 'East'?
ASELECT id FROM sales WHERE amount < (SELECT MAX(amount) FROM sales WHERE region = 'East');
BSELECT id FROM sales WHERE EXISTS (SELECT 1 FROM sales s2 WHERE s2.region = 'East' AND amount < s2.amount);
CSELECT id FROM sales WHERE amount IN (SELECT amount FROM sales WHERE region = 'East');
DSELECT id FROM sales WHERE amount < ANY (SELECT amount FROM sales WHERE region = 'East');
Attempts:
2 left
💡 Hint
Rewrite ANY with an aggregate function to reduce subquery size.
🧠 Conceptual
expert
2:00remaining
Understanding SOME synonym behavior
In PostgreSQL, which statement about the SOME keyword is correct?
ASOME requires the subquery to return only one row
BSOME returns true only if all values satisfy the condition
CSOME cannot be used with NOT operator
DSOME is a synonym for ANY and behaves identically in queries
Attempts:
2 left
💡 Hint
Check PostgreSQL documentation for SOME keyword meaning.

Practice

(1/5)
1. What does the ALL keyword do in a PostgreSQL query with a subquery?
easy
A. Returns all rows from the main query regardless of the subquery.
B. Checks if a condition is true for at least one value returned by the subquery.
C. Checks if a condition is true for every value returned by the subquery.
D. Joins the main query with the subquery on matching columns.

Solution

  1. Step 1: Understand the role of ALL

    The ALL keyword compares a value to every value returned by a subquery.
  2. Step 2: Interpret the condition

    It returns true only if the condition holds for all values from the subquery.
  3. Final Answer:

    Checks if a condition is true for every value returned by the subquery. -> Option C
  4. Quick Check:

    ALL means every value must satisfy condition [OK]
Hint: ALL means every value must meet the condition [OK]
Common Mistakes:
  • Confusing ALL with ANY or SOME
  • Thinking ALL returns all rows without condition
  • Assuming ALL works like a JOIN
2. Which of the following is the correct syntax to use ANY with a subquery in PostgreSQL?
easy
A. SELECT * FROM table WHERE column = ANY (SELECT col FROM othertable);
B. SELECT * FROM table WHERE column ANY = (SELECT col FROM othertable);
C. SELECT * FROM table WHERE ANY column = (SELECT col FROM othertable);
D. SELECT * FROM table WHERE column = (ANY SELECT col FROM othertable);

Solution

  1. Step 1: Recall correct ANY syntax

    ANY is used as: column operator ANY (subquery), e.g., column = ANY (subquery).
  2. Step 2: Check each option

    SELECT * FROM table WHERE column = ANY (SELECT col FROM othertable); matches correct syntax. Others have misplaced ANY or wrong order.
  3. Final Answer:

    SELECT * FROM table WHERE column = ANY (SELECT col FROM othertable); -> Option A
  4. Quick Check:

    ANY goes after operator and before subquery in parentheses [OK]
Hint: Use operator = ANY (subquery) syntax [OK]
Common Mistakes:
  • Placing ANY before the column name
  • Omitting parentheses around subquery
  • Using ANY without an operator
3. Given the tables:
products(id, price)
and
discounts(min_price)
What does this query return?
SELECT id FROM products WHERE price > ALL (SELECT min_price FROM discounts);
medium
A. All product ids regardless of price.
B. Product ids with price greater than every min_price in discounts.
C. Product ids with price equal to any min_price in discounts.
D. Product ids with price greater than at least one min_price in discounts.

Solution

  1. Step 1: Understand the ALL condition

    The condition price > ALL (subquery) means price must be greater than every min_price returned.
  2. Step 2: Interpret the query result

    Only products with price higher than all discount min_prices are selected.
  3. Final Answer:

    Product ids with price greater than every min_price in discounts. -> Option B
  4. Quick Check:

    price > ALL means greater than all values [OK]
Hint: ALL means condition holds for every subquery value [OK]
Common Mistakes:
  • Confusing ALL with ANY or SOME
  • Thinking it returns products with price greater than some min_price
  • Assuming it returns all products
4. Identify the error in this query:
SELECT name FROM employees WHERE salary > ANY SELECT bonus FROM bonuses;
medium
A. Using ANY instead of ALL causes syntax error.
B. Incorrect use of > operator with ANY.
C. Subquery should be in FROM clause, not WHERE.
D. Missing parentheses around the subquery after ANY.

Solution

  1. Step 1: Check ANY syntax

    ANY must be followed by a subquery enclosed in parentheses.
  2. Step 2: Identify missing parentheses

    The query lacks parentheses around SELECT bonus FROM bonuses after ANY.
  3. Final Answer:

    Missing parentheses around the subquery after ANY. -> Option D
  4. Quick Check:

    ANY requires parentheses around subquery [OK]
Hint: Always put subquery in parentheses after ANY [OK]
Common Mistakes:
  • Omitting parentheses around subquery
  • Misplacing ANY keyword
  • Confusing ANY with ALL syntax
5. You want to find all customers whose order total is less than the maximum order total of any customer in the 'orders' table. Which query correctly uses ANY to achieve this?
hard
A. SELECT customer_id FROM orders WHERE total < ANY (SELECT total FROM orders);
B. SELECT customer_id FROM orders WHERE total < ALL (SELECT total FROM orders);
C. SELECT customer_id FROM orders WHERE total > ANY (SELECT total FROM orders);
D. SELECT customer_id FROM orders WHERE total = ANY (SELECT MAX(total) FROM orders);

Solution

  1. Step 1: Understand the goal

    We want customers with order total less than the maximum order total of any customer.
  2. Step 2: Analyze options

    SELECT customer_id FROM orders WHERE total < ANY (SELECT total FROM orders); checks if total is less than any total in orders, which includes the maximum, so it works.
  3. Step 3: Why others are wrong

    SELECT customer_id FROM orders WHERE total < ALL (SELECT total FROM orders); uses ALL, meaning less than every total, which is too strict. SELECT customer_id FROM orders WHERE total > ANY (SELECT total FROM orders); checks greater than any total, wrong direction. SELECT customer_id FROM orders WHERE total = ANY (SELECT MAX(total) FROM orders); compares to MAX(total) with ANY, which is redundant and incorrect.
  4. Final Answer:

    SELECT customer_id FROM orders WHERE total < ANY (SELECT total FROM orders); -> Option A
  5. Quick Check:

    Use ANY to compare with multiple values including max [OK]
Hint: Use ANY to compare with multiple values including max [OK]
Common Mistakes:
  • Using ALL instead of ANY for less than max
  • Comparing with MAX inside ANY incorrectly
  • Confusing > and < operators
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard