Skip to main content

SQL: customers with account in Dharan

Short answer

Join Depositor and Account on acc_no, filter branch_name = 'Dharan', and select DISTINCT customer_name to avoid repeats. For example: SELECT DISTINCT d.customer_name FROM Depositor d JOIN Account a ON d.acc_no = a.acc_no WHERE a.branch_name = 'Dharan'.

Consider database schema: Account (acc_no, branch_name, balance) Branch (branch_name, branch_city, assets) Customer (customer_name, customer_street, customer_city) Loan (loan_no, branch_name, amount) Depositor (customer_name, acc_no) Borrower (customer_name, loan_no) List the name of customers who have acc in dharan branch.

The customer's name is in Depositor, the branch is in Account, and the two are linked by acc_no. So we join Depositor and Account and filter on the branch name. DISTINCT removes repeats when a customer has more than one account in the branch.

Using a join

SELECT DISTINCT d.customer_name
FROM   Depositor d
JOIN   Account   a ON d.acc_no = a.acc_no
WHERE  a.branch_name = 'Dharan';

Using a subquery

SELECT DISTINCT customer_name
FROM   Depositor
WHERE  acc_no IN (SELECT acc_no
                  FROM   Account
                  WHERE  branch_name = 'Dharan');

Relational algebra

Π customer_name (σ branch_name = 'Dharan' (Depositor ⋈ Account))

Sample (tested): Sujan holds accounts 1 and 2, both in Dharan, so the output is just Sujan once.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Database Management System chapter 3 questions →