Joins · Problem 49 of 118

Accounts with Owner and Branch

EasyJoinsBank data

For accounts 1 to 6, return account_id, the customer's full_name as owner, and the branch name as branch, sorted by account_id.

Hint 1

accounts holds both customer_id and branch_id; join one table for each.

Reference solution

SELECT a.account_id,
       c.full_name AS owner,
       b.name AS branch
FROM accounts AS a
JOIN customers AS c ON c.customer_id = a.customer_id
JOIN branches AS b ON b.branch_id = a.branch_id
WHERE a.account_id BETWEEN 1 AND 6
ORDER BY a.account_id;

A child table (accounts) often holds several foreign keys, one for each parent. You join each parent separately. A useful habit: say the join out loud ("an account belongs to a customer and to a branch") and write one ON per sentence.