Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a SQL query for: Who borrowed most and how much? Display customer name and

ID: 3585223 • Letter: W

Question

Write a SQL query for:

Who borrowed most and how much? Display customer name and amount borrowed in your SQL statement.

create table account

(account_number varchar(15) not null,

branch_name varchar(15) not null,

balance number not null,

primary key(account_number));

create table branch

(branch_name varchar(15) not null,

branch_city varchar(15) not null,

assets number not null,

primary key(branch_name));

create table customer

(customer_name varchar(15) not null,

customer_street varchar(12) not null,

customer_city varchar(15) not null,

primary key(customer_name));

create table loan

(loan_number varchar(15) not null,

branch_name varchar(15) not null,

amount number not null,

primary key(loan_number));

create table depositor

(customer_name varchar(15) not null,

account_number varchar(15) not null,

primary key(customer_name, account_number),

foreign key(account_number) references account(account_number),

foreign key(customer_name) references customer(customer_name));

create table borrower

(customer_name varchar(15) not null,

loan_number varchar(15) not null,

primary key(customer_name, loan_number),

foreign key(customer_name) references customer(customer_name),

foreign key(loan_number) references loan(loan_number));

Here is the solution I have:

select borrower.customer_name,loan.amount from borrower INNER JOIN loan ON loan.loan_number = borrower.loan_number having max(loan.amount);

Is there an alternative way to solve this problem?

Explanation / Answer

Yes the alternative way is through cross - product as depicted in the following SQL query:

SELECT borrower.customer_name,loan.amount

FROM borrower , loan

WHERE loan.loan_number = borrower.loan_number

HAVING max(loan.amount);

But the better way to solve the problem is through your SQL Query

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote