Bird
0
0

Which of the following is the correct way to write a subquery with IN in the WHERE clause in PostgreSQL?

easy📝 Syntax Q3 of 15
PostgreSQL - Subqueries in PostgreSQL
Which of the following is the correct way to write a subquery with IN in the WHERE clause in PostgreSQL?
ASELECT * FROM orders WHERE product_id IN (SELECT id FROM products WHERE price > 100);
BSELECT * FROM orders WHERE product_id = IN (SELECT id FROM products WHERE price > 100);
CSELECT * FROM orders WHERE product_id IN SELECT id FROM products WHERE price > 100;
DSELECT * FROM orders WHERE product_id IN (SELECT id FROM products WHERE price > 100
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct syntax for subquery with IN

    The subquery must be enclosed in parentheses and follow the IN keyword directly.
  2. Step 2: Analyze options

    SELECT * FROM orders WHERE product_id IN (SELECT id FROM products WHERE price > 100); correctly uses parentheses and proper syntax.
    SELECT * FROM orders WHERE product_id = IN (SELECT id FROM products WHERE price > 100); has an extra '=' sign.
    SELECT * FROM orders WHERE product_id IN SELECT id FROM products WHERE price > 100; misses parentheses around the subquery.
    SELECT * FROM orders WHERE product_id IN (SELECT id FROM products WHERE price > 100 is missing the closing parenthesis.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Subquery must be inside parentheses after IN [OK]
Quick Trick: Always enclose subqueries in parentheses after IN [OK]
Common Mistakes:
  • Forgetting parentheses around the subquery
  • Using '=' with IN keyword
  • Missing closing parenthesis

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes