Bird
0
0

Given the tables orders(order_id, customer_id, amount) and customers(customer_id, name), what does this query return?

medium📝 query result Q5 of 15
PostgreSQL - Subqueries in PostgreSQL
Given the tables orders(order_id, customer_id, amount) and customers(customer_id, name), what does this query return?
SELECT c.name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id AND o.amount > 100);
ANames of customers with no orders over 100.
BNames of all customers regardless of orders.
CAn error due to missing GROUP BY clause.
DNames of customers who have at least one order with amount over 100.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the EXISTS correlated subquery

    The subquery checks if any order for customer has amount > 100.
  2. Step 2: Determine the outer query result

    Only customers with such orders are returned.
  3. Final Answer:

    Names of customers who have at least one order with amount over 100. -> Option D
  4. Quick Check:

    EXISTS filters customers with qualifying orders = Names of customers who have at least one order with amount over 100. [OK]
Quick Trick: EXISTS returns true if subquery finds any matching row. [OK]
Common Mistakes:
  • Assuming all customers are returned.
  • Confusing EXISTS with COUNT.
  • Expecting GROUP BY error incorrectly.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes