Bird
0
0

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

medium📝 query result Q4 of 15
SQL - Subqueries
Given tables orders(order_id, customer_id, amount) and customers(customer_id, name), what will this query return?

SELECT order_id, (SELECT name FROM customers WHERE customers.customer_id = orders.customer_id) AS customer_name FROM orders;
AList of orders with NULL customer names
BError due to subquery returning multiple rows
CList of orders with corresponding customer names
DList of customers with their order counts
Step-by-Step Solution
Solution:
  1. Step 1: Analyze subquery role

    The subquery fetches the customer name matching each order's customer_id.
  2. Step 2: Understand output

    For each order, the scalar subquery returns one customer name, so the query lists orders with names.
  3. Final Answer:

    List of orders with corresponding customer names -> Option C
  4. Quick Check:

    Scalar subquery returns matching customer name [OK]
Quick Trick: Scalar subquery fetches one matching value per row [OK]
Common Mistakes:
MISTAKES
  • Assuming subquery returns multiple rows causing error
  • Expecting NULLs when matching customer exists
  • Confusing output with aggregation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes