Bird
0
0

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📝 Application Q15 of 15
PostgreSQL - Subqueries in PostgreSQL
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?
ASELECT customer_id FROM orders WHERE total < ANY (SELECT total FROM orders);
BSELECT customer_id FROM orders WHERE total < ALL (SELECT total FROM orders);
CSELECT customer_id FROM orders WHERE total > ANY (SELECT total FROM orders);
DSELECT customer_id FROM orders WHERE total = ANY (SELECT MAX(total) FROM orders);
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes