Bird
0
0

You have two tables:

hard📝 Application Q15 of 15
SQL - INNER JOIN
You have two tables:
Orders(order_id, customer_id, amount)
Customers(customer_id, customer_name)
You want to find all customers who have placed orders and the total amount they spent.
Which query correctly uses INNER JOIN and aggregation to get this result?
ASELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name;
BSELECT c.customer_name, o.amount FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id;
CSELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name;
DSELECT customer_name, amount FROM Customers INNER JOIN Orders ON customer_id = customer_id;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want customers who placed orders and the total amount spent, so INNER JOIN with aggregation is needed.
  2. Step 2: Analyze each option

    SELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name; correctly uses INNER JOIN on customer_id and sums amounts grouped by customer_name. SELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name; uses LEFT JOIN, which includes customers without orders. SELECT c.customer_name, o.amount FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id; does not aggregate amounts. SELECT customer_name, amount FROM Customers INNER JOIN Orders ON customer_id = customer_id; has ambiguous join condition and no aggregation.
  3. Final Answer:

    SELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name; -> Option C
  4. Quick Check:

    INNER JOIN + GROUP BY + SUM = total spent per customer [OK]
Quick Trick: Use INNER JOIN with GROUP BY and SUM for totals [OK]
Common Mistakes:
MISTAKES
  • Using LEFT JOIN instead of INNER JOIN when only matching rows needed
  • Forgetting GROUP BY with aggregation
  • Incorrect join condition syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes