Bird
0
0

Which of the following demonstrates a valid correlated subquery structure in PostgreSQL?

easy📝 Syntax Q3 of 15
PostgreSQL - Subqueries in PostgreSQL
Which of the following demonstrates a valid correlated subquery structure in PostgreSQL?
ASELECT c.name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);
BSELECT * FROM orders WHERE order_id IN (SELECT order_id FROM customers WHERE customer_id = orders.customer_id);
CSELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
DSELECT name FROM departments WHERE id = (SELECT department_id FROM employees);
Step-by-Step Solution
Solution:
  1. Step 1: Identify the outer query and inner subquery

    The outer query is selecting from customers c, and the subquery references c.customer_id, which is from the outer query.
  2. Step 2: Check for correlation

    The subquery uses o.customer_id = c.customer_id, linking the inner query to the outer query, making it a correlated subquery.
  3. Final Answer:

    SELECT c.name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id); correctly shows a correlated subquery.
  4. Quick Check:

    Subquery references outer query alias [OK]
Quick Trick: Correlated subqueries reference outer query columns [OK]
Common Mistakes:
  • Using subqueries without referencing outer query columns
  • Confusing correlated subqueries with simple subqueries
  • Incorrect alias usage causing no correlation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes