The MySQL link offers several ways to solve this problem. The simplest is the subquery:
SELECT * FROM loan l1 WHERE loan_date=(SELECT MAX(l2.loan_date) FROM loan l2 WHERE l1.sss_no = l2.sss_no);
Given that this type of subquery has potentially poor performance , they also suggest using JOIN (essentially the answer of Mahmoud Gamal):
SELECT l1.loan_no, l1.amount, l1.sss_no, l1.loan_date FROM loan l1 JOIN ( SELECT loan_no, MAX(loan_date) AS loan_date FROM loan GROUP BY sss_no) AS l2 ON l1.loan_date = l2.loan_date AND l1.sss_no = l2.sss_no;
The third option:
SELECT l1.loan_no, l1.amount, l1.sss_no, l1.loan_date FROM loan l1 LEFT JOIN loan l2 ON l1.sss_no = l2.sss_no AND l1.loan_date < l2.loan_date WHERE l2.sss_no IS NULL;
LEFT JOIN works on the basis that when l1.loan_date has the maximum value, l2.loan_date appears, so the values ββof the line l2 will be NULL.
All of them should have the same output, but probably differ in performance.
source share