Mysql - Examine last query run by thread in sleep state
First you have to set general logs of mysql by add lines in /etc/mysql/my.cnf 's mysqld section.

general_log     = 1
general_log_file        = /var/log/mysql/mysql.log


Restart mysql -

sudo service mysql restart


If you don't want to restart mysql server-

SET global log_output = 'FILE';
SET global general_log_file='/var/log/mysql/mysql.log';
SET global general_log = 1;


If you wants to read logs from table, mysql database have a general_log table which insert all logs. you can get by- 

   SET global log_output = 'table';
   SET global general_log = 1;
   SELECT * FROM mysql.general_log;


and you can disable by - 

SET global general_log = 0;


Now you can see those process which are in sleep state &amp; get its Id-

show full processlist;


open new tab and see the logs of ID &amp; its running command-

cat /var/log/mysql/mysql.log | grep ID_value


or if you read logs from table then-

 SELECT * FROM mysql.general_log where thread_id=1;


Be careful to use it on Production server, it may effect your server performance (due to additional disk write) if transactions committing on huge no. per second.