查找在Perryridge分行同时拥有帐户和贷款的所有客户

时间:2017-03-29 20:37:20

标签: sql

我有一些表格:

  • "分支" table(branch_name,branch_city,assets)
  • "顾客" table(customer_name,customer_street,customer_city)
  • "帐户" table(account_number,branch_name,balance)
  • " loan" table(loan_number,branch_name,amount)
  • "存款人"表(customer_name,account_number)
  • "借款人" table(customer_name,loan_number)

现在我想找到所有在Perryridge分行同时拥有账户和贷款的客户。

我写的SQL查询是

SELECT distinct customer_name 
FROM borrower, loan
WHERE borrower.loan_number = loan.loan_number 
      AND branch_name = 'Perryridge' 
      AND (branch_name, customer_name )     
          IN (SELECT branch_name, customer_name 
             FROM depositor, account
             WHERE depositor.account_number = account.account_number )

我想知道是否还有其他更简单的SQL查询可以完成相同的问题,例如使用TABLE UNION。

2 个答案:

答案 0 :(得分:2)

try this updated script




SELECT DISTINCT
  customer_name
FROM customer c
INNER JOIN depositor d
  ON d.customer_name=c.customer_name
inner join accounts a on d.account_number = a.account_number
 INNER JOIN borrower b
  ON b.customer_name = c.customer_name
INNER JOIN loan l
  ON l.loan_number = b.loan_number
WHERE l.branch_name = a.branch_name 
AND l.branch_name = 'Perryridge'

答案 1 :(得分:0)

选择不同的客户名称 来自借款人,贷款 WHERE借款人。贷款编号=贷款。贷款编号       AND branch_name ='Perryridge'       AND(分支名称,客户名称)
          IN(选择branch_name,customer_name              从存款人,帐户              在哪里depositor.account_number = account.account_number)