My tables, borrower:
+----------+---------+
| name | loan_id |
+----------+---------+
| Adams | L16 |
| Curry | L93 |
| Hayes | L15 |
| Jackson | L14 |
| Jones | L17 |
| Smith | L11 |
| Smith | L23 |
| Williams | L17 |
+----------+---------+
loan:
+---------+-------------+--------+
| loan_id | branch_name | amount |
+---------+-------------+--------+
| L11 | Round Hill | 900 |
| L14 | Downtown | 1500 |
| L15 | Perryridge | 1500 |
| L16 | Perryridge | 1300 |
| L17 | Downtown | 1000 |
| L23 | Redwood | 2000 |
| L93 | Mianus | 500 |
+---------+-------------+--------+
I wish to find the maximum loan taken by the customer.
I was partially successful with the query:
select name, max(amount)
from loan, borrower
where borrower.loan_id=loan.loan_id;
Result:
+-------+-------------+
| name | max(amount) |
+-------+-------------+
| Adams | 2000 |
+-------+-------------+
But it is taking the name from the first row and finding the maximum amount and printing the result. So I used the having
clause like this:
select name, amount
from loan, borrower
where borrower.loan_id=loan.loan_id
having max(amount)=amount;
But I get an empty set.