Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
table BRANCH(BRANCH_NAME,BRANCH_CITY,ASSETS)

table loan(LOAN_NUMBER,AMOUNT,BRANCH_NAME)









Delete all loans taken from branches located in Needham city.


What I have tried:

delete
from loan l
where b.branch_name=l.branch_name 
and b.branch_city='Needham city'
Posted
Updated 3-Mar-20 8:34am

1 solution

So we want to do some SELECTive deleting of loans based on the Branch City, but we don't have that column in our Loan table, just the Branch Name. Sounds like a good place to start
SQL
DELETE
FROM   Loan
WHERE  Branch_Name -- to be continued
Now we do have a branch table that has the names and cities within it... lets see what this will give us something like this
SQL
SELECT Branch_Name
FROM   Branch
WHERE  Branch_City = 'Needham city'

Now to use this within the DELETE command, we will use the IN to call the SELECT as a subquery
SQL
DELETE
FROM   Loan
WHERE  Branch_Name IN (
     SELECT Branch_Name
     FROM   Branch
     WHERE  Branch_City = 'Needham city'
)
 
Share this answer
 
Comments
Abed Al Rahman Hussien Balhawan 3-Mar-20 23:43pm    
amazing clarification , oracle10g is giving me this error

ORA-02292: integrity constraint (SYS.FK_LOAN_TAKE) violated - child record found
MadMyche 4-Mar-20 6:41am    
That would be the result of a Foreign Key relationship with another table- there is probably a table with payments or balances that references the loan table. The "child" records in that table would need to be deleted BEFORE you can delete the "parent" record in the Loan table
Abed Al Rahman Hussien Balhawan 4-Mar-20 14:08pm    
thank you for the clarification

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900