Skip to content
Advertisement

Get the last entered id from a table in sql

enter image description hereI have an employee table. In that table I have n number of loan.. Now I want to select all the employees with his last taken loan value and date.. can anyone help me out in this.

There’s what I’ve done so far:

SELECT employee_id,employee_name,department_name, 
       designation_name,PF_type,PF_number,Opening_balance,
       MAX(Loan_id),MAX(loan_date), MAX(loan_amount)  
FROM single_roww1
GROUP BY employee_id

I tried group by, limit and order by for this but of no use… I also tried to take the last occurence of the employee_id that also didn’t work..

Advertisement

Answer

Update: to get the full row of max id:

SELECT *
FROM loan
INNER JOIN
  (SELECT max(id) max_id
   FROM loan
   GROUP BY employee_id) x ON x.max_id = loan.id
group by employee_id;

I have to mention that your DB table design without primary key isn’t well designed: https://i.stack.imgur.com/DTYO8.png

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement