Bird
0
0

Given the tables:

medium📝 query result Q4 of 15
PostgreSQL - Subqueries in PostgreSQL
Given the tables:
products(id, name, category_id)
categories(id, name)
What will the following query return?
SELECT name FROM products WHERE category_id IN (SELECT id FROM categories WHERE name = 'Books');
AAn error due to incorrect subquery syntax
BNames of all products regardless of category
CNames of categories named 'Books'
DNames of products that belong to the 'Books' category
Step-by-Step Solution
Solution:
  1. Step 1: Understand the subquery

    The subquery selects category IDs where the category name is 'Books'.
  2. Step 2: Main query filtering

    The main query selects product names where their category_id matches any ID returned by the subquery.
  3. Final Answer:

    Names of products that belong to the 'Books' category -> Option D
  4. Quick Check:

    Subquery filters categories, main query filters products by those categories [OK]
Quick Trick: Subquery filters IDs; main query filters rows using those IDs [OK]
Common Mistakes:
  • Confusing product names with category names
  • Assuming the query returns all products
  • Thinking the query syntax is incorrect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes