Bird
0
0

Given tables:

hard📝 Application Q8 of 15
PostgreSQL - Subqueries in PostgreSQL
Given tables:
items(id, cost)
and
thresholds(limit_cost)
Which query returns items with cost greater than every limit_cost in thresholds?
ASELECT id FROM items WHERE cost IN (SELECT limit_cost FROM thresholds);
BSELECT id FROM items WHERE cost > ANY (SELECT limit_cost FROM thresholds);
CSELECT id FROM items WHERE cost > ALL (SELECT limit_cost FROM thresholds);
DSELECT id FROM items WHERE cost >= SOME (SELECT limit_cost FROM thresholds);
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want items with cost greater than every limit_cost value.
  2. Step 2: Use ALL operator

    The ALL operator ensures the cost is greater than all values returned by the subquery.
  3. Step 3: Analyze options

    SELECT id FROM items WHERE cost > ALL (SELECT limit_cost FROM thresholds); correctly uses cost > ALL (subquery). SELECT id FROM items WHERE cost > ANY (SELECT limit_cost FROM thresholds); uses ANY, which means greater than at least one value, not all. SELECT id FROM items WHERE cost IN (SELECT limit_cost FROM thresholds); uses IN, which checks equality, not greater than. SELECT id FROM items WHERE cost >= SOME (SELECT limit_cost FROM thresholds); uses SOME (synonym for ANY) with >=, which is incorrect for the requirement.
  4. Final Answer:

    SELECT id FROM items WHERE cost > ALL (SELECT limit_cost FROM thresholds); -> Option C
  5. Quick Check:

    Confirm ALL operator matches 'every' condition [OK]
Quick Trick: Use ALL to compare against every value in subquery [OK]
Common Mistakes:
  • Using ANY or SOME instead of ALL
  • Using IN which checks equality
  • Confusing >= with > for ALL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes