Bird
0
0

Consider the query:

medium📝 query result Q5 of 15
PostgreSQL - Subqueries in PostgreSQL
Consider the query:
SELECT product_name FROM products WHERE category_id IN (SELECT id FROM categories WHERE active = true);

What does this query return?
AProduct names in active categories only.
BAll product names regardless of category status.
CProduct names in inactive categories only.
DAn error due to missing JOIN clause.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the subquery

    The subquery selects IDs of categories where active is true.
  2. Step 2: Understand the main query filter

    The main query selects product names where category_id matches any active category ID.
  3. Final Answer:

    Product names in active categories only. -> Option A
  4. Quick Check:

    IN filters products by active categories [OK]
Quick Trick: IN filters main query by subquery results [OK]
Common Mistakes:
  • Assuming query returns all products
  • Thinking JOIN is required for this filter
  • Confusing active and inactive categories

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes