Bird
0
0

Given the tables:

medium📝 query result Q13 of 15
SQL - Subqueries
Given the tables:
users(id, name)
orders(id, user_id, amount)
What will this query return?
SELECT sub.name, sub.total FROM (SELECT u.name, SUM(o.amount) AS total FROM users u JOIN orders o ON u.id = o.user_id GROUP BY u.name) AS sub WHERE sub.total > 100;
ANames of users with total order amount greater than 100
BAll users with their total order amount
CSyntax error due to missing alias
DEmpty result because no users have orders
Step-by-Step Solution
Solution:
  1. Step 1: Understand the subquery

    The subquery calculates total order amount per user by joining users and orders and grouping by user name.
  2. Step 2: Apply the outer WHERE filter

    The outer query filters to only include users whose total order amount is greater than 100.
  3. Final Answer:

    Names of users with total order amount greater than 100 -> Option A
  4. Quick Check:

    Subquery sums orders; outer filters total > 100 [OK]
Quick Trick: Subquery calculates totals; outer query filters results [OK]
Common Mistakes:
MISTAKES
  • Ignoring the WHERE filter on total
  • Assuming all users are returned
  • Confusing alias usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes